PHP Tutorial File Cache Instance Code
Caching is widely used in real-world applications, which reduces access to the server database tutorials and increases the speed of operation. At present, many CMS content management systems frequently use caching mechanism to improve the efficiency of system operation.
The cache.php code is as follows:
PHP code
/*
Constants that the user needs to define beforehand:
_cachepath_ Template Cache Path
_cacheenable_ whether the automatic caching mechanism is on, undefined, or empty, which means turning off the automatic caching mechanism
_recachetime_ automatic re-cache interval, in seconds, undefined or empty, to turn off automatic re-caching
*/
Class Cache {
var $cachefile;
var $cachefilevar;
function cache () {
Generates the cache group file name for the current page $this->cachefilevar and file name $this->cachefile
The parameters of the dynamic page are different for the cache file, but all the cache files for each dynamic page have the same file name, but the extension is different
$s =array (".", "/"), $r =array ("_", "");
$this->cachefilevar=str_replace ($s, $r, $_server["Script_name"]). " _ ". $_get[_actionvar_];
$this->cachefile= $this->cachefilevar. ".". MD5 ($_server["Request_uri"]);
}
Delete the current page/module cache
function Delete () {
Delete the cache for the current page
$d = Dir (_cachepath_);
$strlen =strlen ($this->cachefilevar);
Returns all cache filegroups for the current page
while (false!== ($entry = $d->read ())) {
if (substr ($entry, 0, $strlen) = = = $this->cachefilevar) {
if (!unlink (_cachepath_. " /". $entry)" {echo "cache directory cannot be written to"; exit;}
}
}
}
Determine if the cache has been removed, and whether the cache is required
function Check () {
If the cache update interval is set _recachetime_
if (_recachetime_+0>0) {
Returns the last update time of the cache for the current page
$var = @file (_cachepath_. ") /". $this->cachefilevar); $var = $var [0];
Delete the cache file if the update time is longer than the update interval
if (Time ()-$var >_recachetime_) {
$this->delete (); $ischage =true;
}
}
Returns the cache for the current page
$file =_cachepath_. " /". $this->cachefile;
Determines whether the current page cache is present and the cache function is turned on
Return (File_exists ($file) and _cacheenable_ and! $ischange);
}
Read cache
function Read () {
Returns the cache for the current page
$file =_cachepath_. " /". $this->cachefile;
Read the contents of the cache file
if (_cacheenable_) return ReadFile ($file);
else return false;
}
Generate cache
function Write ($output) {
Returns the cache for the current page
$file =_cachepath_. " /". $this->cachefile;
If the cache function is turned on
if (_cacheenable_) {
Write the contents of the output to the cache file
fopen ($file, ' W ' "> $fp = @fopen ($file, ' w ');
if (! @fputs ($FP, $output)) {echo "Template cache write failed"; exit;}
@fclose ($FP);
If the cache update interval is set _recachetime_
if (_recachetime_+0>0) {
Update the last update time of the cache for the current page
$file =_cachepath_. " /". $this->cachefilevar;
$fp = @fopen ($file, ' w ');
if (! @fwrite ($FP, Time ())) {echo "The cache directory cannot be written to"; exit;}
@fclose ($FP);
}
}
}
}
?>
Use procedure:
PHP code
Define ("_cachepath_", "./cache/");
Define ("_cacheenable_", "1");
Define ("_recachetime_", "43200");
Include (' cache.php ');
$cache =new cache ();
if ($cache->check ()) {
$template = $cache->read ();
}else {
Ob_start ();
Ob_implicit_flush (0);
?>
Page content ....
$template = Ob_get_contents ();
$cache->write ($template);
}
?>
http://www.bkjia.com/PHPjc/444788.html www.bkjia.com true http://www.bkjia.com/PHPjc/444788.html techarticle PHP Tutorial File Cache Instance code cache is widely used in the actual use, can alleviate the server database tutorial access to improve the speed of operation. Currently many CMS Content management Systems ...