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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With