2012-02-28 55 views
3

我正在尋找緩存以及如何在Doctrine中使用它。Zend Framework - Doctrine2 - 知識庫查詢緩存

我有我的Zend框架的bootstrap.php如下:我用我的數據庫運行一個非常簡單的查詢

// Build Configuration 
$orm_config = new \Doctrine\ORM\Configuration(); 

// Caching 
$cacheOptions = $options['cache']['backendOptions']; 
$cache = new \Doctrine\Common\Cache\MemcacheCache(); 
$memcache = new Memcache; 
$memcache->connect($cacheOptions['servers']['host'], $cacheOptions['servers']['port']); 
$cache->setMemcache($memcache); 
$orm_config->setMetadataCacheImpl($cache); 
$orm_config->setQueryCacheImpl($cache); 
$orm_config->setResultCacheImpl($cache); 

self::_instance()->_em->getRepository('UserManagement\Users')->find('1'); 

而且我不確定如果我正確使用緩存,因爲使用它(如上面配置中的 ),查詢似乎需要兩倍的時間才能執行 ,因爲它被禁用,是嗎?

由於提前, 史蒂夫

回答

4

我似乎已經整理這個自己,那種有關enter link description here。基本上,從我所瞭解的存儲庫查詢如下:

self::_instance()->_em->getRepository('UserManagement\Users')->find('1'); 

不會緩存結果。如果在整個腳本處理過程中再次執行相同的查詢,它將不執行搜索並使用它在內存中的結果 - 這與使用Memcache的實際緩存不同。

實現這一目標的唯一途徑,是覆蓋在自定義庫中的教義EntityRepository找到()方法是這樣的:

public function find($id) 
{ 
    // Retrieve an instance of the Entity Manager 
    $qb = $this->getEntityManager()->createQueryBuilder(); 

    $qb->select('u') 
     ->from('UserManagement\Users', 'u') 
     ->where('u.id = :id') 
     ->setParameter('id', $id); 

    $query = $qb->getQuery(); 
    $query->useResultCache(TRUE); 
    $result = $query->getSingleResult(); 
    return $result; 
} 

值得注意的是,從上面的最重要的行是$query->useResultCache(TRUE); - 這通知應用程序緩存結果。

希望這會有所幫助。