8

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!

1
  • could u please let me know how exactly u r able to check the cache is working using the profiler, specifically USING THE PROFILER. I tried checking the amount of database queries and since I´m getting the same amount of queries every time, before and after the cache is supposedly carried out, so I don´t know if it´s working or if it´s supposed to do that, anyway, hints?? Commented Sep 17, 2015 at 15:47

1 Answer 1

30

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.