Skip to content

Instantly share code, notes, and snippets.

@thrashr888
Last active March 24, 2018 03:32
Show Gist options
  • Save thrashr888/a77193a558215de5f769d3610f374bd9 to your computer and use it in GitHub Desktop.
Save thrashr888/a77193a558215de5f769d3610f374bd9 to your computer and use it in GitHub Desktop.
php file cache example
<?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