- 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 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
- */
- 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() 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 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.' ';
- }
- }
- ?>
复制代码
- demo.php:
-
- /*
- * 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.
- * Author: jiangjun8528@163.com,Junin
- * Author Page: http://blog.csdn.Net/sdts /
- * Last Modify : 2007-8-22
- * Remark :
- 1. This version is Php5. I have not written a Php4 version yet. Please refer to the modifications if necessary (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, ignore the 2nd item above.
- * Some thoughts about caching:
- * The fundamental difference between dynamic caching and static caching is that it is automatic. The process of user accessing the page is to generate cache. The process of browsing cache and updating cache does not require manual intervention.
- * Static caching refers to generating static pages. Related operations are generally completed in the background of the website and require manual operation (that is, manual generation).
- */
- /*
- * Usage examples
- ------------------------------------------------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, update the cache through xx.Php?cacheact=rewrite, and so on, you can also set some other operations
- $cache-> ;load(); //Load the cache. If the cache is valid, the following page code will not be executed
- //The page code begins
- echo date('H:i:s jS F');
- //The page code ends
- $cache-> ;write(); //First run or cache expires, generate cache
- --------------------------------- ---Demo2-------------------------------------------------
- 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, update the cache through xx.Php?cacheact=rewrite, and so on. You can set some other operations
- $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 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, update the cache through xx.Php?cacheact=rewrite, and so on, you can also set some other operations
- $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 cache expiration, generate cache
- */
- ?>
Copy code
|