Last active
March 24, 2018 03:32
-
-
Save thrashr888/a77193a558215de5f769d3610f374bd9 to your computer and use it in GitHub Desktop.
php file cache example
This file contains 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 | |
function get_cache($key) { | |
$cacheFolder = '/tmp/wp-site/'; | |
$cacheFile = $cacheFolder+$key | |
if(file_exists($cacheFile) { | |
$contents = file_read($cacheFile); | |
return json_decode($contents); | |
} else { | |
return false; | |
} | |
} | |
function set_cache($key, $value="") { | |
$cacheFolder = '/tmp/wp-site/'; | |
$cacheFile = $cacheFolder+$key; | |
$contents = json_encode($value); | |
file_write($cacheFile, $contents); | |
} | |
$cacheKey = 'sip-page-endpoint'; | |
if (!$page = get_cache($cacheKey)) { | |
$page = getPageStuff(); | |
set_cache($cacheKey, $page) | |
} | |
echo $page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment