默认情况下,Make Doctrine使用结果缓存 [英] Make Doctrine use result cache by default
问题描述
我将Memcache绑定到Doctrine,似乎我必须在每个查询中显式地使用 useResultCache
。默认情况下是否有可能 true
,可以使用 useResultCache(false)
不需要它? / p>
我知道这个问题是旧的,但我会写出我想到的最好的答案。
1)摘录您对依赖关系(即 - 使用依赖注入模式将EntityManager注入到您的类中,创建查询并使用EntityManagerInterface)
现在,或者:
a)[更好,但更长]为EntityManagerInterface创建一个新的组合相关实现,它将代理对原始entityManager的调用,将结果缓存标志设置为true:
<?php
class CachedEntityManager实现EntityManagerInterface {
private $ proxiedManager;
public function __construct(EntityManagerInterface $ proxiedManager){
$ this-> proxiedManager = $ proxiedManager;
}
public function createQuery($ dql =''){
$ query = $ this-> proxiedManager-> createQuery($ dql);
$ query-> useResultCache(true);
}
[...代理所有的呼叫到proxiedManager ...]
}
b)[不太好,但较短]扩展EntityManager类并覆盖createQuery。记住,这通常不是一个好的做法,你绝对不应该在该类中写任何东西,而是重构为a):
<?php
class CachedEntityManager extends EntityManager {
public function createQuery($ dql =''){
$ query = parent :: createQuery($ dql);
$ query-> useResultCache(true);
}
}
I'm binding Memcache to Doctrine and it seems I have to useResultCache
explicitly in every query. Is it possible to make it true
by default, with the ability to useResultCache(false)
where it's not needed?
I know this question is old, but I'll write up the best answer that comes into my mind.
1) Abstract away your dependency to interface ( i.e. - use dependency injection pattern to inject EntityManager into your class that creates queries and use EntityManagerInterface instead )
Now, either:
a) [ Better, but longer ] Create a new composition-related implementation for EntityManagerInterface, that will proxy calls to original entityManager and will set result cache flag to true:
<?php
class CachedEntityManager implements EntityManagerInterface {
private $proxiedManager;
public function __construct(EntityManagerInterface $proxiedManager) {
$this->proxiedManager = $proxiedManager;
}
public function createQuery($dql = '') {
$query = $this->proxiedManager->createQuery($dql);
$query->useResultCache(true);
}
[... proxy all the calls forth to proxiedManager ...]
}
b) [ Not as good, but shorter ] Extend the EntityManager class and override the createQuery. Remember that this in general is not a good practice and you should definitely not write anything in that class anymore but instead refactor into a) :
<?php
class CachedEntityManager extends EntityManager {
public function createQuery($dql = '') {
$query = parent::createQuery($dql);
$query->useResultCache(true);
}
}
这篇关于默认情况下,Make Doctrine使用结果缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!