Answer the question
In order to leave comments, you need to log in
Articles about caching in PHP
Caching in PHP is still a dark area for me. This is largely due to the fact that all articles are divided into two categories:
1) Now we will tell you about output buffering ... (what I thought of myself :) )
2) Working with memcache and loaded projects (which exceeds my needs at the moment)
Meanwhile, look at the codes of various CMS: everything is much more diverse there. In addition to caching the whole page, individual modules, query results can be cached, that's all I'm most interested in. But I'm not very good at understanding the implementation from the code.
I saw the theoretical articles, the practice would be how to write it better, so that later you don’t have to rewrite, periodically swearing :)
Answer the question
In order to leave comments, you need to log in
Caching is very difficult. If you make changes to the database, then a lot of problems with invalidation appear. It is very easy to forget to insert an invalidation somewhere, even if the framework / ORM helps with this.
All the "fully automated" caching solutions created huge problems because in some cases they didn't invalidate the cache when needed. Let's say there is a Profile object with a balance property. The user buys services, an invoice is generated, and either the balance is shown cached or the invoice does not appear in the list of cached invoices. Both cases cause vehement indignation of users, believe my experience.
It is also difficult to foresee everything manually. In a complex system, there are surprisingly many places where the cache needs to be invalidated, and it is very difficult to foresee them all (without turning the code into spaghetti).
In addition, there is the question of efficiency. Invalidate too often - efficiency is lost, you have to update the data from the database too often. Invalidate too rarely - users complain about outdated data.
With heavy queries, too, everything is difficult. Let's say there is a very heavy request that is not necessarily invalidated, therefore it can be easily and simply cached. But the first user will still have to wait for it to complete. And if the execution result is different for different users, then everyone will have to wait at least once. Nobody will like it either.
In general, there are no ready-made templates for all occasions. Only for read-only data, and then with restrictions. It is desirable to think about caching from the very beginning of the project, then it will be more difficult.
well, there are a few general properties of this caching
where to cache
-To files
-To memory
-To the database (it happens)
-Where else (there are such inventions oooohoo)
What to cache
- Results of query execution (Something in models)
- Ready html (Directly along with requests with javascript and with everything everything everything)
- In general, you can cache anything, any object that can be serialized.
It remains only to figure out in which cases which combination should be used.
The caching mechanism is quite simple. It doesn’t matter what dataSource we have, we have an interface to anyone we use it (So implemented in Zend_Cache)
there is a key, there is a value, tags and cache lifetime
For each key-value pair, we can assign a tag and a certain lifetime
. Tags are needed, for example, to clear the cache on them. (Again, this is implemented in Zend_Cache)
the algorithm is simple:
We look at the key whether there is such a value in the cache, if there is, we get it, if not, we get it from somewhere else and put it into the cache.
The key can be composed according to different principles, starting from the id of an object or id + another id + another id,
or you can generally use a hash from an sql query.
Read about memcache, if you need something smaller, try APC, and in general, everything can be stored in files just in case. Be careful with files, I once had a story when the cache took up all the disk space, as a result of my mistake.
In general, such things are, nothing complicated, good luck!
Caching methods are very dependent on the situation. If in one case putting the result of the request in memcache is good, then in the other it will be a clear evil. But there can be a great many cases ... There is only practice.
In theory, something like a “wrapper” around caching methods can help here. I put it in the memcache settings, looked at how it works. Then I changed it to memcachedb, for example, and looked again ... Well, according to the results, choose a specific caching method for a specific case.
You need to take existing code and see how others are doing it. Zend_Cache and ADODB have good caching in my opinion, also PEAR_Cache, and the code is good. It does not mean to take as a basis, but to read and understand how.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question