A PHP Cache Class code (with detailed description)

Source: Internet
Author: User
Tags define chmod mkdir
Copy CodeThe 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 ');
$CacheName =md5 ($_server[' Request_uri ')). Cache_fix; Cached file name
$CacheDir =cache_root. ' /'. substr ($CacheName, 0, 1);//cache file storage Directory
$CACHEURL = $CacheDir. ' /'. $CacheName;//The full path of the cached file
Get-mode requests are cached and generally want to see the latest results after post
if ($_server[' request_method ']== ' get ') {
If the cached file exists and does not expire, read it.
if (file_exists ($CacheName) && time ()-filemtime ($CacheName) <cache_time) {
$FP =fopen ($CacheName, ' RB ');
Fpassthru ($FP);
Fclose ($FP);
Exit
}
To determine if a folder exists, if it does not exist, create
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 automatically calls this function when the program ends
function Autocache ($contents) {
Global $CACHEURL;
$FP =fopen ($CACHEURL, ' WB ');
Fwrite ($fp, $contents);
Fclose ($FP);
chmod ($CACHEURL, 0777);
When the new cache is generated, all the 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{
Deletes the cached file if it is not a request for get.
if (file_exists ($CACHEURL)) unlink ($CACHEURL);
}
?>


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.