Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache life time in LswMemcacheBundle

I decided to use LswMemcacheBundle (in Symfony2) to cache Doctrine2 queries into memcached, but I've got one problem. I can't find any information about possibility to change cache life time, even about default life time.

Is there anyone who can provide me such informations?

like image 279
Elon Than Avatar asked Apr 27 '26 03:04

Elon Than


2 Answers

It looks like results cache is not enabled for all queries (but query cache is enabled) and we need to enable it using useResultCache method. This method also allows us to set cache life time.

So it'll look like this

$em->createQuery('SELECT a FROM SomeBundle:Entity a')
      ->useResultCache(true, 3600, 'cacheId')
      ->getResult();

and for createQueryBuilder

$repository = $this->getEntityManager()->getRepository('SomeBundle:Entity');
$qb = $repository->createQueryBuilder('g');
$qb->andWhere('g.categoryId = :categoryId')->setParameter('categoryId', '1');
$qb->addOrderBy('g.id', 'DESC');

$query = $qb->getQuery();
$query->useResultCache(true, 3600, 'cacheId');

where 3600 will be cache life time in seconds and cacheId our cache key.

like image 110
Elon Than Avatar answered Apr 28 '26 18:04

Elon Than


I think that you still have to set the cache lifetime using the methods through the cacheDriver.

http://docs.doctrine-project.org/en/2.0.x/reference/caching.html#result-cache

http://docs.doctrine-project.org/en/2.0.x/reference/caching.html#saving

like image 40
Ken Hannel Avatar answered Apr 28 '26 17:04

Ken Hannel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!