Cronjob with cached file

2

On my site and using cpanel, I have several cronjobs that run multiple php files. Each of these cronjobs creates a cached file where the site will fetch information. All 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 pick up values from multiple sources and this process is time consuming. It does everything accordingly and the update 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 bad luck to consult the page that depends on that cache during the update process, it will end up giving up because it does not seem to work.

I have tried solutions but not success.

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 finished with all new values already updated, it only replaces the current cached file.

Can anyone give me a hint how to get this?

    
asked by anonymous 20.01.2017 / 02:57

2 answers

2

Modify the scheduling processes so that, while searching for new information, instead of deleting the current cache, generate the new data in a new file. Once you have completed this new file rename it with the name of the main file, which will be replaced.

1. Main cache file: "data.txt"
2. Cron executes the processes and saves in "data-new.txt" 3. When you complete the new data: rename "data-new.txt" to "data.txt".

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

    
20.01.2017 / 07:29
0

It seems to me that it is just the same flow problem. If you can not consider another caching technique (if your system is actually dependent on that cache medium), the best solution is the one you mentioned yourself: caching is only done when the cron has been completed. If the cache you use is simple (HTML only), consider using tools like PHP OP Cache . If your site is in WordPress the WPSuperCache might be a good solution.

    
20.01.2017 / 13:39