Use default Doctrine result cache

I am binding Memcache to Doctrine, and it seems like I should explicitly useResultCachein every request. Is it possible to do this trueby default, with the option useResultCache(false)where it is not needed?

+3
source share
3 answers

I know this question is old, but I will write the best answer that comes to my mind.

1) Abandon your dependency on the interface (for example, use the dependency injection pattern to inject the EntityManager into your class that creates queries and uses EntityManagerInterface instead)

Now either:

a) [, ] , EntityManagerInterface, entityManager 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):

<?php
class CachedEntityManager extends EntityManager { 

public function createQuery($dql = '') {
    $query = parent::createQuery($dql);
    $query->useResultCache(true);   
}

}
+3

/-, useResultCache(true) , .

+8

​​Doctrine, $_useResultCache TRUE \Doctrine\ORM\AbstractQuery. resultCacheDriver , , $query->useResultCache(FALSE)

This is a useful little hack that will save you a lot of seals, but be careful; I found that the caching driver will not cache lazy loaded associations that have not been initialized (which is obvious now, I'm thinking about it). Sometimes it’s safer to just enable result caching for each individual query.

+2
source

All Articles