Cronjob with cached file

Asked

Viewed 42 times

2

On my website and using Cpanel, I have several cronjobs that run multiple php files. Each of these cronjobs creates a cached file where the site will pick up information. All the cronjobs are working fine, but I’m having a problem with one of them. This cronjob that I’m talking about, takes about 1.5 minutes to update the cached file because it will get values from various sources and this process is time consuming. It does everything accordingly and the upgrade is perfect, but ...

The problem is that the cached file is deleted as soon as cronjob is started and the new cache file will only appear after all values have been collected (1.5 minutes later). Therefore, if a user of the site has the misfortune to refer to the page that depends on this cache during the update process, he will eventually give up because he thinks it is not working.

I have tried solutions but unsuccessfully.

The solution that seems to me the best, would be this: - the cached file remains in use while a new cache is being created through a temporary file. - when this temporary file is completed with all new values already updated, only then it replaces the current cache file.

Can someone give me a hint on how to get this?

  • Seems to me it’s just flow problem anyway. If you cannot consider another caching technique (if your system is in fact dependent on this cache medium), the best solution is the one you yourself mentioned: the cache file is overwritten only when the cron task is completed. If the cache you use is simple (HTML only), consider using tools such as PHP OP Cache. If your site is in Wordpress the Wpsupercache may be a good solution.

  • Not at all. I put the comment in response, since it helped you.

2 answers

2

Modify the scheduling processes so that, while searching for new information, instead of deleting the current cache, you generate the new data in a new file. As soon as you complete this new file rename it with the main file name, which will be overwritten.


1. Main cache file: "data.txt"
2. cron executes processes and saves to "data-new.txt"
3. When completing new data: rename "data-new.txt" to "data.txt".

It doesn’t mean that this is the only or the best way to solve it. Depending on how the data structure is, the processes, etc., there may be other means but as this is not clear in the question, this is a more obvious option.

  • This technique is for almost everything, for FTP (when updating the renamed files after it is already on the server), for database (when you have to drop a very large table, create one with the same renamed structure and delete).

  • Simple and effective. + 1

0

Looks to me like it’s just a flow problem anyway. If you cannot consider another caching technique (if your system is in fact dependent on this cache medium), the best solution is the one you yourself mentioned: the cache file is overwritten only when the cron task is completed. If the cache you use is simple (HTML only), consider using tools such as PHP OP Cache. If your site is in Wordpress the Wpsupercache may be a good solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.