使 Doctrine 默认使用结果缓存 [英] Make Doctrine use result cache by default

查看:28
本文介绍了使 Doctrine 默认使用结果缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Memcache 绑定到 Doctrine,看来我必须在每个查询中显式地 useResultCache.是否可以默认设置为 true,并能够在不需要的地方 useResultCache(false)?

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) 抽象出对接口的依赖(即 - 使用依赖注入模式将 EntityManager 注入到创建查询的类中,并改用 EntityManagerInterface )

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 )

现在,要么:

a) [更好,但更长] 为 EntityManagerInterface 创建一个与组合相关的新实现,它将代理对原始 entityManager 的调用并将结果缓存标志设置为 true:

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) [ 不那么好,但更短] 扩展 EntityManager 类并覆盖 createQuery.请记住,这通常不是一个好的做法,您绝对不应该再在该类中编写任何内容,而是重构为 a):

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);   
}

}

这篇关于使 Doctrine 默认使用结果缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆