php - Use WP Cron to Clear a Page Cache?

admin2025-01-08  4

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

Share Improve this question edited Jan 1, 2019 at 4:39 Justin Waulters 3422 silver badges8 bronze badges asked Dec 31, 2018 at 19:09 Best Dev TutorialsBest Dev Tutorials 4451 gold badge7 silver badges20 bronze badges 2
  • What happens if no one looks at it? In other words, if no one visits the page for six hours, is it still important to cache the page 3 times? or can you just skip the first two caches and do it once in the sixth hour? – John Dee Commented Dec 31, 2018 at 19:37
  • @JohnDee If nobody looks at it, it's not important. But, I figured I would just have the page re-cache (by "visiting" it) every 2 hours. That way if someone DOES come across the page, they see a cached version. The page takes about 5 seconds to load without cache (using PHP 7 on a VPS). It uses a bunch of file_get_contents() as a temporary solution that will be removed in a month or so. – Best Dev Tutorials Commented Dec 31, 2018 at 19:45
Add a comment  | 

2 Answers 2

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266449a1121.html

最新回复(0)