php - simple cache feature - retrieve data every hour

I want to set up a simple cache feature with php. I want the script to get data from somewhere, but not to do it on every page view, but only every hour.

I know i can have a cron job that runs a php script every hour.

But I was wondering if this can be achieved without cron, just inside the php script that created the page based on the data fetched (or cached). I'm really looking the simplest solution possible. It doesn't have to be accurate


I would use APC as well, but in either case you still need some logic. Basic file cache in PHP:

if (file_exists($cache_file) and time() - filemtime($cache_file) < 3600)
    $content = unserialize(file_get_contents($cache_file));
    $content = your_get_content_function_here();
    file_put_contents($cache_file, serialize($content));

You only need to serialize/unserialize if $content is not a string (e.g. an array or object).

Why just don't use APC ? you can do


And then you can retrive the content with:


Need Your Help