Skip to content

Instantly share code, notes, and snippets.

@dmgig
Created July 11, 2016 20:32
Show Gist options
  • Save dmgig/0fbd2cf37634495e1c1de581515c1734 to your computer and use it in GitHub Desktop.
Save dmgig/0fbd2cf37634495e1c1de581515c1734 to your computer and use it in GitHub Desktop.
Simple PHP Cache
<?php
$cacheDir = "cache";
$cacheFile = $cacheDir."/data-cache-".md5($_SERVER['QUERY_STRING']).".json";
$generFile = "data-generate.php";
if(!is_writable($cacheDir)){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
if (file_exists($cacheFile) and (time() - filemtime($cacheFile)) < 3600)
{
$content = file_get_contents($cacheFile);
}
else
{
ob_start();
include $generFile;
$content = ob_get_clean();
$res = file_put_contents($cacheFile, $content);
touch($cacheFile);
}
echo $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment