Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing query cache in Symfony2 / Doctrine

I've recently written my first Symfony2 app and all is well, except now I want to add in some query caching to improve performance and cut down on unneeded queries. I've added the following lines to one particular query's builder:

$query->useResultCache(true)
      ->useQueryCache(true);

After the first request, the cache is then used as expected. I can verify that in the profiler. All is great!

The problem is I also have a simple admin panel I wrote that allows the user to modify the content, but after changes the cached version is still being used.

Is there a way I can 'programmatically' tell Symfony2 / Doctrine to clear the query cache as I update the data, or is there a way of configuring this?

It seems like it would be a common issue but I can't find anything on Google relating to the issue!

like image 844
Adam Avatar asked Mar 21 '13 22:03

Adam


1 Answers

I recommend using result cache id - that way you can clear one particular result cache:

$query->setResultCacheId('my_custom_id');
// or shorter notation with lifetime option
$query->useResultCache(true, 3600, 'my_custom_id');

// to delete cache
$cacheDriver = $entityManager->getConfiguration()->getResultCacheImpl();
$cacheDriver->delete('my_custom_id');
// to delete all cache entries
$cacheDriver->deleteAll();

For more info on cache deleting see:
http://docs.doctrine-project.org/en/latest/reference/caching.html#deleting

like image 92
tomas.pecserke Avatar answered Sep 20 '22 22:09

tomas.pecserke