I have a pretty complicated index.php
now, and I would like to only run it once every hour. What is the best way to achieve this? Some ideas I've had
apc_store($page, 60*60*)
- I feel this isn't what APC is for and will probably be doing something bad to the other parts of my siteWhen the next visitor comes, first look for the saved file, and if it exists, serve that rather than execute all the code in your index.php file. A basic example would be
if (file_exists($cacheFileName))
{
require $cacheFileName;
exit;
}
// here goes the rest of your index.php code
//..
// assuming your output is buffered and is contained in $output:
echo $output;
$cacheFileName = '/path/to/your/file.inc';
file_put_contents($cacheFileName, $output);
Set up a cron job that will delete your saved cache file from disk every hour or as often as you need to. Alternatively, in your index.php, on every page hit check how long ago the cached file was created and generate a new cache file if it's been around longer than you'd want it to. A cron job is easier to set up though.
To answer the deep philosophical question though, saving generated output to disk in separate files is probably the best way if you don't want to rely on third party caching solutions. APC is good for caching the code that will regenerate pages when needed, and memcached is definitely overkill if we're talking about a small(ish) application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With