Skip to content

Instantly share code, notes, and snippets.

@dtbaker
dtbaker / query.php
Last active August 29, 2015 14:09
Hacky caching with PHP
$key = $_SERVER['REMOTE_ADDR'] . serialize($_REQUEST);
if(strlen($key) < 500){ // help prevent flooding.
$key = md5($key);
// global so we can find it in the shutdown function
$GLOBALS['wordpress_temp_file'] = dirname(__FILE__).'/cache/wp_'.basename($key);
if(is_file($GLOBALS['wordpress_temp_file']) && filemtime($GLOBALS['wordpress_temp_file']) > (time() - 3600)){
$data = unserialize(file_get_contents($GLOBALS['wordpress_temp_file']));
echo $data['content'];
exit;
}