<?php
define('CACHE_PATH', $_SERVER["DOCUMENT_ROOT"]."/cachefiles/");
// how long to keep the cache files (hours)
define('CACHE_TIME', 12);
// return location and name for cache file
function cache_file()
{
return CACHE_PATH . md5($_SERVER['REQUEST_URI']);
}
// display cached file if present and not expired
function cache_display()
{
$file = cache_file();
// check that cache file exists and is not too old
if(!file_exists($file)) return;
if(filemtime($file) < time() - CACHE_TIME * 3600) return;
// if so, display cache file and stop processing
readfile($file);
exit;
}
// write to cache file
function cache_page($content)
{
if(false !== ($f = @fopen(cache_file(), 'w'))) {
fwrite($f, $content);
fclose($f);
}
return $content;
}
// execution stops here if valid cache file found
cache_display();
// enable output buffering and create cache file
ob_start('cache_page');
?>
上面的代码使用随机名称创建缓存
例如
4c556ca729a88177e72946a4c3732f62
a87aef8e1d11cee944a8854ab8377ac6
85f2e557d6b483fc06db804d35e6580f
它如何使它存储具有实际页面名称的缓存页面,例如
主页-4c556ca729a88177e72946a4c3732f62
关于-a87aef8e1d11cee944a8854ab8377ac6
我-85f2e557d6b483fc06db804d35e6580f