Home > Backend Development > PHP Tutorial > PHP file caching example code_PHP tutorial

PHP file caching example code_PHP tutorial

WBOY
Release: 2016-07-20 11:09:36
Original
951 people have browsed it

php tutorial file caching example code
Cache is widely used in actual use, which can reduce access to the server database tutorial and improve running speed. Currently, caching mechanisms are frequently used in many CMS content management systems to improve the efficiency of system operation.
cache.php code is as follows:
php code
/*
Users need to define it in advance Constants:
_cachepath_ Template cache path
_cacheenable_ Whether the automatic caching mechanism is turned on, undefined or empty, indicating that the automatic caching mechanism is turned off
_recachetime_ Automatic re-caching interval, in seconds, undefined or empty, Indicates turning off automatic recaching
*/

class cache {

var $cachefile;
var $cachefilevar; //Generate the cache group file name $this->cachefilevar and file name $this->cachefile of the current page
//The cache files corresponding to different parameters of the dynamic page are also different, but all caches of each dynamic page The files all have the same file name, but have different extensions
$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_); = $d->read())) {
If (substr($entry,0,$strlen)==$this->cachefilevar) {
If (!unlink(_cachepath_."/" .$entry)) {echo "cache directory cannot be written";exit;}
                                                                                                                                   >function check() {
                                                                                                                                                  $var =@file(_cachepath_."/".$this->cachefilevar);$var=$var[0];
                                                                                                          )-$var>_recachetime_) {
                                                                          cache
$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 the cache of the current page
$file=_cachepath_."/".$this->cachefile;
                                                                                                                                                         >} 
 
//Generate cache 
function write($output) { 
                                                                                                                                            ;
                                                                                                                                                                   $file,'w');
                                                                                                                                                                                                     //If the cache update interval is set_recachetime_
                                                   $file=_cachepath_."/".$this ->cachefilevar;
                             $fp=@fopen($file,'w');                                                                    ;} 
                                                                                                                                       Usage process:
php code
define("_cachepath_","./cache/");
define("_cacheenable_","1");
define("_recachetime_","43200");
include('cache.php');
$cache=new cache();
if ($cache->check()) {
$template=$cache->read (); Page content. . . .
$template = ob_get_contents();
$cache->write($template);
} }
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444788.htmlTechArticlephp tutorial file caching example code caching is widely used in actual use and can alleviate access to the server database tutorial. Improve running speed. At present, many cms content management systems...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template