Caching is widely used in actual use, which can reduce access to the server database and improve operating speed. At present, many CMS content management systems frequently use caching mechanisms to improve the efficiency of system operation. Below is a well-written cache class. You can refer to the cache mechanism and writing method.
cache.php code is as follows:
[php] view plaincopy
-
-
/*
-
Constants that users need to define in advance:
-
_CachePath_ Template cache path
-
_CacheEnable_ Whether the automatic caching mechanism is enabled, undefined or empty, indicating that the automatic caching mechanism is turned off
-
_ReCacheTime_ Automatic re-caching interval, in seconds, undefined or empty, indicating that automatic re-caching is turned off
-
*/
-
-
class cache
-
{
-
var $cachefile;
-
var $cachefilevar;
-
-
function cache()
-
//Generate the Cache group file name $this->cachefilevar and file name $this->cachefile of the current page -
// The parameters of the dynamic page are different and the corresponding Cache files are also different, but all the Cache files of each dynamic page have the same file name, but the extensions are 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 cache of the current page/module -
function delete() -
-
//Delete the cache of the current page
$d = dir(_CachePath_);
-
$strlen=strlen($this->cachefilevar);
//Return all Cache file groups of the current page-
-
If (substr($entry,0,$strlen)==$this->cachefilevar) -
-
If (!unlink(_CachePath_."/".$entry)) {echo "The Cache directory cannot be written";exit;}
-
-
}
-
-
//Determine whether it has been cached and whether it needs to be cached
-
Function check()
-
//If the cache update interval is set _ReCacheTime_-
If (_ReCacheTime_+0>0) -
-
// Return to the last update time of Cache on the current page
-
$var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];
-
// If the update time exceeds the update interval, delete the Cache file
If (time()-$var>_ReCacheTime_) -
-
$ This-& gt; delete (); $ ischage = true;
-
-
//Return to the Cache of the current page
$file=_CachePath_."/".$this->cachefile;
-
//Determine whether the cache of the current page exists and whether the Cache function is enabled
-
return (file_exists($file) and _CacheEnable_ and !$ischange);
} -
-
//Read Cache -
Function read() -
-
//Return to the Cache of 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)
-
//Return to the Cache of the current page
-
$file=_CachePath_."/".$this->cachefile;
//If the Cache function is turned on
-
If (_CacheEnable_)
-
//Write the output content into the Cache file
-
$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 current page Cache -
$file=_CachePath_."/".$this->cachefilevar; -
$fp=@fopen($file,'w');
-
If (!@fwrite($fp,time())) {echo "Cache directory cannot be written";exit;}
-
@fclose($fp);
-
} -
} -
?> -
-
-
-
Class usage:
[php]
view plaincopy
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/750358.html
- www.bkjia.com
- true
- http: //www.bkjia.com/PHPjc/750358.html
TechArticle
Cache is widely used in actual use, which can reduce access to the server database and improve operating speed. At present, many CMS content management systems frequently use caching mechanisms to improve the system...