When should data caching be used?
For example, when querying from the database, and if the info from the database is updated 5 times during the lifetime of the cache, then the cache will not be erased?
Of course, we can set the cache lifetime? And if the info from the database will be constantly updated?
Indicate, please, cases in which caching should be applied.

2 Answers 2

The cache should be used to reduce the load on the database(query frequency, computational complexity) and increase the speed of returning the result with this data.
The cache lifetime must be selected according to the situation.There must always be a trigger by which the old cache is invalidated and new data is cached so as not to store incorrect data.For very frequent data updates, the cache may be pointless.
  • Thank you so much!
    And in the case of goods in the store, what cache db?
    – Agreeable32 Oct 12 '19 at 00:33
  • [[Hfnas]], unfortunately, did not develop online stores) When on your project it was necessary to cache regular pages with content, then they cached for 1 hour, the invalidation was either in time(that is, the cache’s lifetime expired and it was generated again) or by content update(i.e.in the page refresh action the cache was reset).If you put too long a life time(day, week), then the cache may grow too much – Aggressive33 Oct 12 '19 at 00:40
A cache is needed when the data does not change very often, and their calculation is more expensive than storing them in a ready-made form.For example, page templates, what's the point of generating them every time from some kind of Smarty in PHP, if they are static?
  • Thank you very much, do not cache static pages, but dynamic-cache, do I understand you correctly?
    And how do you determine the cache lifetime?
    – Agreeable32 Oct 12 '19 at 01:00
  • [[Hfnas]], not only cache pages, you can cache a lot in many different ways.
    The lifetime just depends on these factors, well, and on how often the data is updated.
    – All-Star91 Oct 12 '19 at 01:57