A PHP Cache Class code (with detailed instructions) _php tutorial

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

  • 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.