-
- 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 is 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 if the cache is valid
- * edit bbs.it-home.org
- */
- 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 to the cache
- * $mode == 0, obtain the page content through the browser cache
- * $mode == 1, obtain the page content through direct assignment (received through the $content parameter)
- * $mode = = 2, get the page content by reading it locally (fopen ile_get_contents) (it seems that 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 the cache
- * exit() After loading the cache, terminate the execution of the original page program. If the cache is invalid, run the original page program to generate a cache
- * ob_start() Enable the browser cache to obtain the 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('清除缓存文件失败!请检查目录权限!');
- }
- }
- /**
- * 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('所设定缓存目录不存在并且创建失败!请检查目录权限!');
- 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.' ';
- }
- }
- ?>
复制代码
2. Demonstration code of php cache class
-
-
/* - * Can be freely reproduced and used, please retain 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, delete cache.
- * Remark:
- 1. This version is Php5 version
- 2. This version is utf-8 encoding, if the website uses other Please convert the encoding yourself. For Windows systems, use Notepad to open and save as, and select the corresponding encoding (generally ANSI). For Linux, please use the corresponding editing software or the iconv command line.
- 3. If you copy and paste, ignore item 2 above. .
- * Some thoughts about caching:
- * The fundamental difference between dynamic caching and static caching is that it is automatic. The process of user accessing a page is the process of generating cache, browsing cache, and updating cache, without manual intervention.
- * Static caching It refers to generating static pages. The related operations are usually completed in the background of the website and require manual operation (that is, manual generation).
- */
-
- /*
- * Examples of usage
- ----------Demo1 --------
- require_once('cache.inc.php');
- $cachedir = './Cache/'; //Set the cache directory
- $cache = new Cache($cachedir,10); //Omit the parameters and use the default settings, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //Here is a trick, through xx.Php?cacheact =rewrite updates the cache
- $cache->load(); //Load the cache. If the cache is valid, the following page code will not be executed
- //The page code starts
- echo date('H:i:s jS F');
- / /End of page code
- $cache->write(); //First run or cache expiration, generate cache
---------Demo2------ -
- require_once('cache.inc.php');
- $cachedir = './Cache/'; //Set the cache directory
- $cache = new Cache($cachedir,10); //Omit the parameter and use the default Provincial settings, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //Here is a trick, update the cache through xx.Php?cacheact=rewrite
- $cache ->load(); //Load the cache. If the cache is valid, the following page code will not be executed
- //The page code starts
- $content = date('H:i:s jS F');
- echo $content;
- / /End of page code
- $cache->write(1,$content); //First run or cache expiration, generate cache
------------- Demo3----
- require_once('cache.inc.php');
- define('CACHEENABLE',true);
-
- if (CACHEENABLE) {
- $cachedir = './Cache/'; //Set cache Directory
- $cache = new Cache($cachedir,10); //Omit the parameters to adopt the default settings, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //Here is a trick, update the cache through xx.Php?cacheact=rewrite
- $cache->load(); //Load the cache, if the cache is valid, the following page code will not be executed
- }
- //The page code starts
- $content = date('H:i:s jS F');
- echo $content;
- //End of page code
- if (CACHEENABLE)
- $cache->write(1,$content); //First run Or the cache expires, generate cache
- */
- ?>
-
Copy code
|