Created
July 11, 2016 20:32
-
-
Save dmgig/0fbd2cf37634495e1c1de581515c1734 to your computer and use it in GitHub Desktop.
Simple PHP Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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