I'm using a plugin (Comet Cache) to save static versions of a particular page I have on my site.
Inside the plugin, I have the cache set to clear every 2 hours (that way this page can show the latest, non-cached) version of data its pulling from.
The only issue is, for this page to get cached again, someone has to manually browser to it.
I'm trying to automate this with WP Cron where it triggers a page load of the content. I've also been thinking of doing this with Python outside of WordPress.
Would there be a best practices way of doing this? Thanks
I'm using a plugin (Comet Cache) to save static versions of a particular page I have on my site.
Inside the plugin, I have the cache set to clear every 2 hours (that way this page can show the latest, non-cached) version of data its pulling from.
The only issue is, for this page to get cached again, someone has to manually browser to it.
I'm trying to automate this with WP Cron where it triggers a page load of the content. I've also been thinking of doing this with Python outside of WordPress.
Would there be a best practices way of doing this? Thanks
Yes, using WP-CRON will work for this well. One way to do it, is to set a new cron for immediately with every page load. Do not give the job a unique name. By default, WP-CRON will only take an action once every ten minutes. So in the case where your site is being hit a lot, the cron will run every ten minutes or so. It runs its's own process, so it shouldn't slow your system down. The 2 hour idea is a waste because it's more work for no gain.
The benefit is that it's easy, and will update the cache every ten minutes. The drawback is that if NO ONE visits the site, the first page-hit will be the old cached data [which will then initiate an immediate cron]. Note if this data is only accessible to logged in viewers, or you have to click a menu or ANYTHING, that will set off the cron in the background.
So yeah, it's a good approach, probably the best approach to load cached data in the background.
There is no way to make the WP Cron system work without the website being hit. If you would like to prevent the user from having to be the one to initiate the cache refresh you can set up an actual scheduled task with something like AWS Lambda or CronJob.org to hit the page every minute.
However, even if the scheduled task runs every minute, there is still a small window of close-to-a-minute where a user might still be the one to initiate the WP Cron.
A better solution, if your cache system has the proper hooks, is to set up the scheduled task to hit a url with a query string something like ?cache_refresh=true
and then parse that to refresh the cache (or expire it and set it again) so that a user will never be the one to initiate the refresh. With this method, you would set your expiration time a lot higher (like a day) and then make the scheduled task run every two hours to give the scheduled task full control over when the cache is refreshed. In doing so, you would no longer need to use the WP Cron system at all.
If your plugin does not have the hooks to do this, you may want to check to see if it is using Wordpress Transients to achieve the caching. If it is, you may be able to use the Transient API to force the refresh - or just scrap the plugin all together and use the Transient API directly with the scheduled tasks to have better control over the process.
EDIT: Additionally, maybe a bit overkill for this application, you could make your WP Cron activate an asynchronous task that will do the work in the background - most easily done with a library such as this one.