Answer the question
In order to leave comments, you need to log in
How to properly use cache in Yii?
There are a lot of opportunities to use the cache, but in what cases they are not said anywhere in practice.
I'm making a simple blog site, with one table for articles, without the "update_time" column.
As I imagine the mechanics - after the creation of the article, it (the request and its appearance) is cached for a long time. After editing or deleting - an action is activated that resets the cache for this article. At the next request, it is cached again until better times.
The questions are:
Answer the question
In order to leave comments, you need to log in
The cache is very easy to use.
When you are sure that the work with data is 100% optimized, and still the speed is unsatisfactory, then you need to start using the cache.
It follows from this, in particular, that until the site traffic reaches at least 20K visitors a day, you do not need any cache
Not right.
Selections by primary key should never be cached at all.
Your misconception is that you believe the database is some kind of clumsy monster that will not take a step without caching. This is, to put it mildly, not true. Most requests don't need to be cached at all. And fetching one row by primary key will always be fast, no matter how busy the site is.
I have not worked with caching in Yii, but since 1.5 years ago I have been implementing it for
CodeIgniter
. elements, and requesting information from a third-party service... There can be many things, but the main criterion is that this code fragment takes a long time to execute
2) You execute your "heavy" code once and store its result in the cache
3) Further, when requesting the same information you check if there are results for it in the cache and if its time has not expired
4) If both conditions are OK - you get the information from the cache, and do not execute your "heavy" code again
Usually, the cache is used in two cases:
1) Reduce the load for any service
2) Increase the speed of loading the site (application)
In general, the "get from the cache" operation is, in fact, reading a txt file, if you do not go into implementation details.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question