Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Doctrine or Symfony Cache?

I have been reading extensively about Doctrine different options for Caching as well as symfony caching mechanisms:

Symfony Official: https://symfony.com/doc/4.0/components/cache.html

Doctrine Official: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html

KnP university (very useful as always): https://knpuniversity.com/screencast/symfony-fundamentals/caching

Other good resources: https://blog.kaliop.com/blog/2014/10/06/doctrine-symfony2-2/

Nevertheless, despite explaining HOW to use the cache system, I can’t figure out WHEN to use the cache. Under which circumstances is the cache very useful and when not to use it.

For example, in my project, I have a large amount of data to pull from my database that I’d like to cache (pulling entities with tons of left joins). Those left joins are, for some, updated every hour, every day or every minutes on regular basis though a bot called with a cron job (symfony command).

I don’t know how make sure all my data are updated properly when I display it to the user with the cache mechanism enabled? If the DB gets updated do I need to remove the data from the cache manually calling for example $cacheDriver->delete('my_data’); at time of update and checking if the data exist then save it anew when retrieving the data? Would this be the proper way to do it?

Also, should I use Doctrine Cache or Symfony 4 cache? Which one to choose from? I have an example of one of the query I’d like to cache on another SO thread here : https://stackoverflow.com/a/51800728/1083453

Bottom line would be, how to make that query as efficient as possible?

I’m leaning toward the following:

  • Remove cache when updating any data included in my query.
  • Cache it when calling the query for the first time
  • Retrieve it whenever calling the same query between 2 updated

Am I on the right path?

like image 291
Miles M. Avatar asked Dec 17 '25 14:12

Miles M.


2 Answers

There's no official rules. Sometimes you may think you are doing optimization when at best you're losing time and at worst your losing performances because the process of checking if you have cache and if it is valid is longer than the query itself.

Assuming that you have done a fine enough job architecturing your application (meaning there are no inneficient operations, that your queries are well done and don't load useless datas etc), it's really a case by case study.

Try testing the pages of your application with a software simulating hundreds of clients accessing it. You'll soon enough identify which pages can't handle the charge and the debugger will tell you which queries are slowing them down. Those are the ones that definitly will benefit from caching.

like image 97
Magnesium Avatar answered Dec 20 '25 08:12

Magnesium


I was wondering the same thing and stumbled upon your question.

Full disclosure: I've never used doctrine result cache :) One thing I expect is that with Doctrine result cache, you don't need to bother with serializing / deserializing your cached data. This could be very convenient, when you're trying to cache and later retrieve a complex entity.

like image 39
Wojciechem Avatar answered Dec 20 '25 06:12

Wojciechem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!