Adam
Adam

Reputation: 997

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!

Upvotes: 8

Views: 10325

Answers (1)

tomas.pecserke
tomas.pecserke

Reputation: 3260

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

Upvotes: 30

Related Questions