C
C
ColdSpirit2015-03-26 18:48:37
Yii
ColdSpirit, 2015-03-26 18:48:37

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:

  1. Will there be any problems with my caching option?
  2. How to cache views? I did n't find any information on the Internet, but I didn't understand anything here = (
  3. How to use CHttpCacheFilter with everything else? Is he needed?
  4. Is there any risk in this use case?

Look like that's it. The topic is new for me, so I'll be happy with
everything
) 20K per day is not expected. But the question remains - either this will jump in traffic, or I will start developing a busy site. At least you should not bypass this topic when you intend to study the engine.
Also, I think that a reasonable caching setting will benefit even a site with low traffic. Or am I wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanatPHP, 2015-03-27
@FanatPHP

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.

M
Maxim Kudryavtsev, 2015-03-26
@kumaxim

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.

E
Eugene, 2015-03-28
@Nc_Soft

If you have reached the stage of caching views, then it is better to cache pages with a web server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question