A php Cache code (detailed description) _ PHP Tutorial

Source: Internet
Author: User
Tags php define
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.