A php Cache code (detailed description ). Copy the code as follows :? Bytes
The code is as follows:
Define ('cache _ root', dirname (_ FILE _). '/cache'); // CACHE storage directory
Define ('cache _ time', 1800); // The cache time unit is 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; // complete cache file path
// GET requests are cached, and the latest results are usually expected after POST.
If ($ _ SERVER ['request _ method'] = 'get '){
// Read the cached file if it does not expire.
If (file_exists ($ CacheName) & time ()-filemtime ($ CacheName) $ Fp = fopen ($ CacheName, 'RB ');
Fpassthru ($ fp );
Fclose ($ fp );
Exit;
}
// Determine whether the folder exists. if the folder does not exist, it is created.
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, which is automatically called when the program ends
Function AutoCache ($ contents ){
Global $ CacheUrl;
$ Fp = fopen ($ CacheUrl, 'wb ');
Fwrite ($ fp, $ contents );
Fclose ($ fp );
Chmod ($ CacheUrl, 0777 );
// When a new cache is generated, all old caches are automatically deleted to save space, which 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 cached file if it is not a GET request.
If (file_exists ($ CacheUrl) unlink ($ CacheUrl );
}
?>
The http://www.bkjia.com/PHPjc/323583.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323583.htmlTechArticle code is as follows :? Php define ('cache _ root', dirname (_ FILE __). '/cache'); // cache storage directory define ('cache _ time', 1800); // cache time unit: seconds define('cache_fix', '.html...