Caching with cron jobs - Is this necessary in yii2 ?

I am working on some old site that is caching its content with cron jobs. Now I have to upgrade it, and I want to use yii2, but I do not understand do I have to run yii2 caching scripts or something with cron jobs ?

Is it caching, or pre-optimizing content (or both?)

I can see if you were producing summary data you would not want to calculate it realtime for the user, but run it on an off-peak time.

So a few ways to go here.

You could check cache, and if empty, produce the content. This might be really slow for the first run. Instead of making the user wait (and php timeout) you can run that as a scheduled job and populate the cache ahead of time.

Thanks for your reply. So if I understand, if you have some big amount of data to push into cache, you assign it to cron job, instead of caching it on runtime ?

Yes. Just depends how long it takes.

A few examples i’ve seen/heard of:

-Generating thumbnails of images, used for browsing. Would take way too long to generate all of them the first time if they didn’t already exist. Of course, with these you may try other approaches still like generating the thumbnail on image upload.

-Generating statistics from raw data that are stored for later use, either in cache, or in a database table

You don’t necessarily have to run these as a console app cronjob either. Lets say you have a URL which will do the work like /site/createData. You can program this controller action to accept a known key, say a 32 char hash. When the URL is accessed (‘site/createData/key/ssdf786sd5f744sdf756dsf8dsafds6585ds77’) and passes the auth check, the job runs.

Many ways to go.

Thanks. I got confused when I saw cache dependencies.

Is it better to use dependencies, like last update time or time interval, or cron job ?

cache can be diferrent.

if it just static content from db, low on change - cache into files or even db. cron probably just saves whole page into file, then you include it into layout.

low on change and high on request - cache also into memory. you can use several caching apis at the same time.

yii2 has filesystem caching. to precache page you just have to request it before user.

will it be cron+curl or other “pinger” doesn’t matter

Think of dependencies as a shortcut, nothing more. They just make managing caching easier.

Lets imagine the creators of Yii had not thought of this. How would you manage your cache data?

Well… you would manually check and see if there was new content each request. Perhaps you would run a query to see what the max database ID was for that user (this is what the dependency is doing behind the scenes). Or, you would manually invalidate the cache and re-save it with each change.

On one of my projects, I require the user’s data on the page to always be ‘live’ but I don’t want to suffer unnecessary queries and the slowdown that it causes. You might think there is no way to cache this, because even with a 60 second TTL you would have stale data. What I do in this instance is to clear the cache on saving any changes and reset the cache.

When loading the page for the user, it will always pull from cache this way. If they save something, we regenerate the cache as part of the save() call. The next time they visit, they get a cached copy.

I guess you’d call this manual cache invalidation. Works really well when the amounts of views (reads) vs. writes (saves) is high. If changes happened with every page view this would be a bad strategy.