V
V
Volodymyr Godyak2014-10-15 10:00:27
PHP
Volodymyr Godyak, 2014-10-15 10:00:27

How to implement a cache flush mechanism?

Situation. One of our old projects started loading mysql server. It has 40 thousand products, and attendance is 12 thousand per day. The main load was the filter of goods in the catalog. The cost of goods is formed on the basis of 8 pricing models, which have their own set of dependencies. I perevairiv indexes, added missing, removed unnecessary. The site began to work several times faster. PDO driver. Additionally, I decided to cache mysql queries and results. The caching mechanism works like this:
Before querying the database, a check is made to see if the file exists in the cache folder. If so, the contents of the file are pulled. File structure: file name - md5 from sql query, file content - serialized array of two parameters. The first is the cache expiration time (expired), the second is the content of the result of the sql query (result). Caching works great. But. How can I implement a mechanism for automatically clearing the cache when editing a product or category in the places where they are used. The system is modular. One and the same item may be present in search results, quick search, category browsing, etc. Now it's just in the admin panel a button to clear the cache, which deletes all the cache. The cache is written for 12 hours. Perhaps you have any thoughts on how it can be improved. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2014-10-15
@wmgodyak

file name - md5 from sql query, file content - serialized array of two parameters

Congratulations, you have almost invented a regular mysql Query cache, it works in exactly the same way, only in memory. But which has no problems with results invalidation and non-cached requests.
How caching is done normally: Normal caching has no lifetime. Generally. A normally working cache is invalidated only due to external conditions (something has been changed in the product - invalidated). Or by LRU (i.e., the memory is over)
You cache the goods each separately from the others. Disabled because elementary, what has changed - that's what you dump. And so that there are not a couple of hundred cache misses out of the blue - on the contrary, do not invalidate the cache - but update it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question