Search code examples
phpcachingcronapc

Considerations for scheduling PHP APC


I have a webpage which queries a number of services (Amazon, itunes, etc) for product information per product. There are 25 products per page, which meant this was a very slow process. I implemented apc_add and apc_fetch functions which store the raw output from each service with a key in the form service|country_code|product_id this all works well but the caches need to be expired after a few hours in each case. This tends to mean that every once in a while, someone will get a slow-loading page.

Is there a way, perhaps by using a cron job, I can get the server to do the heavy lifting of updating the caches periodically?

Or should I consider a completely different approach altogether?


Solution

  • You can run a cron job that calls the php page from the command line php command every so often (perhaps with a parameter telling it to skip output) and that way the cache will refresh every period you choose.

    You could even give a parameter (could be the same one as before) that forces refresh when you run from the command line and thus the heavy duty work will be done only at the server.

    In this case you don't retire the cache automatically but only when the cron job runs.