Home >Backend Development >PHP Tutorial >A PHP cache class_PHP tutorial

A PHP cache class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:09857browse

 cache.inc.php:

 
 
class Cache {
   /**
* $dir: cache file storage directory
* $lifetime: cache file validity period, in seconds
* $cacheid: cache file path, including file name
* $ext: Cache file extension (optional), used here for the convenience of viewing files
*/
   private $dir;
   private $lifetime;
   private $cacheid;
   private $ext;
   /**
* Destructor, check whether the cache directory is valid, default assignment
*/
   function __construct($dir=,$lifetime=1800) {
       if ($this->dir_isvalid($dir)) {
           $this->dir = $dir;
           $this->lifetime = $lifetime;
           $this->ext = .Php;
           $this->cacheid = $this->getcacheid();
       }
   }
   /**
* Check whether the cache is valid
*/
   private function isvalid() {
       if (!file_exists($this->cacheid)) return false;
       if (!(@$mtime = filemtime($this->cacheid))) return false;
       if (mktime() - $mtime > $this->lifetime) return false;
       return true;
   }
   /**
* Write cache
* $mode == 0, obtain page content through browser cache
* $mode == 1, obtain the page content by direct assignment (received through the $content parameter)
* $mode == 2, get the page content by reading locally (fopen ile_get_contents) (it seems this method is unnecessary)
*/
   public function write($mode=0,$content=) {
       switch ($mode) {
           case 0:
               $content = ob_get_contents();
               break;
           default:
               break;
       }
       ob_end_flush();
       try {
           file_put_contents($this->cacheid,$content);
       }
       catch (Exception $e) {
           $this->error(写入缓存失败!请检查目录权限!);
       }
   }
   /**
* Load cache
* exit() terminates the execution of the original page program after loading the cache. If the cache is invalid, run the original page program to generate a cache
* ob_start() turns on browser caching to obtain page content at the end of the page
*/
   public function load() {
       if ($this->isvalid()) {
           echo "This is Cache. ";
           //以下两种方式,哪种方式好?????
           require_once($this->cacheid);
           //echo file_get_contents($this->cacheid);
           exit();
       }
       else {
           ob_start();
       }
   }
   /**
* Clear cache
*/
   public function clean() {
       try {
           unlink($this->cacheid);
       }
       catch (Exception $e) {
$this->error(Failed to clear cache files! Please check directory permissions!);
}
}
/**
* Get cache file path
*/
private function getcacheid() {
return $this->dir.md5($this->geturl()).$this->ext;
}
/**
* Check if the directory exists or can be created
*/
private function dir_isvalid($dir) {
if (is_dir($dir)) return true;
try {
mkdir($dir,0777);
}
catch (Exception $e) {
$this->error(The set cache directory does not exist and the creation failed! Please check the directory permissions!);
            return false;                            
}
return true;
}
/**
* Get the complete url of the current page
*/
private function geturl() {
$url = ;
if (isset($_SERVER[REQUEST_URI])) {
$url = $_SERVER[REQUEST_URI];
}
else {
$url = $_SERVER[Php_SELF];
$url .= empty($_SERVER[QUERY_STRING])?:?.$_SERVER[QUERY_STRING];
}
return $url;
}
/**
* Output error message
*/
private function error($str) {
echo
.$str.
;
}
}
?>
demo.php:
/*
* You can freely reprint and use it, please keep the copyright information, thank you for using!
* Class Name : Cache (For Php5)
* Version : 1.0
* Description: Dynamic cache class, used to control the page to automatically generate cache, call cache, update cache, and delete cache.
* Author: jiangjun8528@163.com,Junin
* Author Page: http://blog.csdn.Net/sdts/
* Last Modify : 2007-8-22
* Remark :
1. This version is the Php5 version. I have not written the Php4 version yet. If necessary, please refer to the modification by yourself (it’s easier, don’t be so lazy, haha!).
2. This version is encoded in utf-8. If the website uses other encodings, please convert it yourself. On Windows systems, use Notepad to open and save as, and select the corresponding encoding (generally ANSI). Under Linux, please use the corresponding editing software or iconv Command line.
3. If you copy and paste, you don’t need to worry about item 2 above.
* Some thoughts about caching:
* move

http://www.bkjia.com/PHPjc/486010.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486010.htmlTechArticlecache.inc.php: ?php class Cache { /** * $dir: cache file storage directory* $ lifetime: cache file validity period, unit is seconds* $cacheid: cache file path, including file name* $ext: cache...
Statement:
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