Copy CodeThe code is as follows:
Define (' Cache_root ', DirName (__file__). ' /cache '); Cache Storage Directory
Define (' Cache_time ', 1800);//cache time Unit seconds
Define (' Cache_fix ', '. html ');
$CacheName =md5 ($_server[' Request_uri '). Cache_fix; Cache file Name
$CacheDir =cache_root. ' /'. substr ($CacheName, 0, 1);//cache file storage Directory
$CACHEURL = $CacheDir. ' /'. $CacheName;//full path to the cache file
Get-mode requests are cached and generally expect to see the latest results after post
if ($_server[' Request_method ']== ' GET ') {
If the cache file exists and does not expire, read it out.
if (file_exists ($CacheName) && time ()-filemtime ($CacheName)
$FP =fopen ($CacheName, ' RB ');
Fpassthru ($FP);
Fclose ($FP);
Exit
}
Determines whether the folder exists, does not exist, and creates
ElseIf (!file_exists ($CacheDir)) {
if (!file_exists (Cache_root)) {
mkdir (cache_root,0777);
chmod (cache_root,0777);
}
mkdir ($CacheDir, 0777);
chmod ($CacheDir, 0777);
}
callback function that calls this function automatically when the program ends
function Autocache ($contents) {
Global $CACHEURL;
$FP =fopen ($CACHEURL, ' WB ');
Fwrite ($fp, $contents);
Fclose ($FP);
chmod ($CACHEURL, 0777);
While generating a new cache, all old caches are automatically deleted to save space and can be ignored.
Deloldcache ();
return $contents;
}
function Deloldcache () {
ChDir (Cache_root);
foreach (Glob ("*/*"). Cache_fix) as $file) {
if (Time ()-filemtime ($file) >cache_time) unlink ($file);
}
}
callback function Auto_cache
Ob_start (' Autocache ');
}else{
Delete the cache file instead of the GET request.
if (file_exists ($CACHEURL)) unlink ($CACHEURL);
}
?>
http://www.bkjia.com/PHPjc/323583.html www.bkjia.com true http://www.bkjia.com/PHPjc/323583.html techarticle Copy the code as follows: PHP define (' Cache_root ', DirName (__file__). ' /cache '); Cache storage Directory define (' Cache_time ', 1800);//cache time Unit seconds define (' Cache_fix ', '. html ...