- cache.inc.php:
-
- Class Cache {
- /**
- * $dir: Cache file storage Directory
- * $lifetime: Cache file validity, in seconds
- * $cacheid: Cache file path, including file name
- * $ext: Cache file extension (not available), used here for convenient viewing of 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 Cache
- * $mode = = 0, get page content in browser cache
- * $mode = = 1, get the page content in a direct assignment (received via the $content parameter)
- * $mode = = 2, get the page content in a local read (fopen ile_get_contents) way (it seems that this way is not necessary)
- */
- 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 (' Write cache failed! Please check directory permissions! ');
- }
- }
- /**
- * Load Cache
- * EXIT () to terminate the execution of the original page program after loading the cache, and run the original page program generation cache if the cache is invalid
- * Ob_start () Open browser cache to get page content at the end of the page
- */
- Public function load () {
- if ($this->isvalid ()) {
- echo "This isCache. ";
- Here are two ways to do it?????
- 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 (' Erase cache file failed! Check directory permissions! ');
- }
- }
- /**
- * Get cache file path
- */
- Private Function Getcacheid () {
- return $this->dir.md5 ($this->geturl ()). $this->ext;
- }
- /**
- * Check whether 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 (' Set cache directory does not exist and failed to create! Please check the directory permissions! ');
- return false;
- }
- return true;
- }
- /**
- * Get the full 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. ';
- }
- }
- ?>
Copy Code
- demo.php:
- /*
- * Can be freely reproduced use, please keep the copyright information, thank you for using!
- * Class Name:cache (for PHP5)
- * version:1.0
- * Description: Dynamic cache class for controlling 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 version, I do not write PHP4 version, if necessary, please refer to the modification (easier, not so lazy, hehe!).
- 2. This version is UTF-8 encoding, if the website uses other encoding, please convert, Windows system with Notepad open Save As, select the appropriate encoding (general ANSI), under Linux use the appropriate editing software or ICONV command line.
- 3. Copy and paste the above 2nd is not the tube.
- * Some thoughts about caching:
- * The fundamental difference between a dynamic cache and a static cache is that it is automatic, and the user's access to the page process is the process of generating the cache, browsing the cache, updating the cache, without manual intervention.
- * Static cache refers to the generation of static pages, related operations are generally done in the background of the site, manual operation (that is, manually generated).
- */
- /*
- * Examples of how to use
- ------------------------------------Demo1-------------------------------------------
- Require_once (' cache.inc.php ');
- $cachedir = './cache/'; Set Cache Directory
- $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
- if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
- $cache->load (); Load cache, cache is valid do not execute the following page code
- Page code Start
- echo Date (' h:i:s JS F ');
- Page code End
- $cache->write (); First run or cache expiration, generating cache
- ------------------------------------Demo2-------------------------------------------
- Require_once (' cache.inc.php ');
- $cachedir = './cache/'; Set Cache Directory
- $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
- if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
- $cache->load (); Load cache, cache is valid do not execute the following page code
- Page code Start
- $content = Date (' h:i:s JS F ');
- Echo $content;
- Page code End
- $cache->write (1, $content); First run or cache expiration, generating cache
- ------------------------------------Demo3-------------------------------------------
- Require_once (' cache.inc.php ');
- Define (' cacheenable ', true);
- if (cacheenable) {
- $cachedir = './cache/'; Set Cache Directory
- $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
- if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
- $cache->load (); Load cache, cache is valid do not execute the following page code
- }
- Page code Start
- $content = Date (' h:i:s JS F ');
- Echo $content;
- Page code End
- if (cacheenable)
- $cache->write (1, $content); First run or cache expiration, generating cache
- */
- ?>
Copy Code
|