gpt4 book ai didi

doctrine-orm - Zend Framework - Doctrine2 - 存储库查询缓存

转载 作者:行者123 更新时间:2023-12-01 12:56:36 24 4
gpt4 key购买 nike

我正在研究缓存以及如何在 Doctrine 中使用它。

我的 Zend Framework 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');

而且我不确定我是否正确使用了缓存,因为启用它(如根据上面的配置)查询似乎需要两倍的时间来执行就像它被禁用一样,这是对的吗?

提前致谢,史蒂夫

最佳答案

这似乎是我自己整理的,与 enter link description here 有点相关.基本上,据我了解,存储库查询如下:

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

不会缓存结果。如果在整个脚本处理过程中再次执行相同的查询,它将不会执行搜索并使用它在内存中的结果 - 这与真正的缓存不同,在我的例子中使用 Memcache。

实现这一点的唯一方法是在自定义存储库中覆盖 Doctrine EntityRepository find() 方法,如下所示:

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); - 这会通知应用程序缓存结果。

希望这对您有所帮助。

关于doctrine-orm - Zend Framework - Doctrine2 - 存储库查询缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9481186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com