How to create a simple PHP cache script

Last updated on
$cache_file =  getcwd().'/filename.cache';

if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 9 ))) {

    header('Content-Type: application/json');
    echo file_get_contents($cache_file);

} else {

    $feed = "content to cache";

    file_put_contents($cache_file, $feed, LOCK_EX);
    echo $feed;

}

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

Your email address will not be published. Required fields are marked *