Clearing request cache in Symfony2 / Doctrine

I recently wrote my first Symfony2 application, and everything is fine, but now I want to add query caching to improve performance and reduce unnecessary queries. I added the following lines to one specific query builder:

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

After the first request, the cache is used as expected. I can check this in the profiler. All perfectly!

The problem is that I also have a simple admin panel that I wrote that allows the user to change the contents, but after the changes the cached version is still in use.

Is there a way to programmatically tell Symfony2 / Doctrine to clear the query cache when updating data, or is there a way to configure it?

It seems like this will be a common problem, but I can not find anything on Google on this problem!

+6
source share
1 answer

I recommend using the result cache identifier - this way you can clear one specific 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 information on deleting the cache, see:
http://docs.doctrine-project.org/en/latest/reference/caching.html#deleting

+20
source

All Articles