Cache. inc. php:
<? 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), which is used here for File Viewing convenience */private $ dir; private $ lifetime; private $ cacheid; private $ ext; /*** destructor: Check whether the cache directory is valid. The default value is */function _ construct ($ dir =, $ lifetime = 1800) {if ($ this-> dir_isvalid ($ dir) {$ this-> dir = $ dir; $ this-> lifetime = $ lifetime; $ this-> ext =. php; $ this-> cacheid = $ this-> getca Cheid () ;}}/*** 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 the page content in browser cache * $ mode = 1 and assign values directly (received by the $ content parameter) obtain the page content in the form of * $ mode = 2, and obtain the page content 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 (write cache failed! Check the directory permission !); }/*** Load cache * exit () Terminate the execution of the original page program after loading the cache. If the cache is invalid, run the original page program to generate the cache * ob_start () enable browser cache to get page content at the end of the page */public function load () {if ($ this-> isvalid () {echo "<span style = display: none;> This is Cache. </span> "; // which of the following two methods is better ????? 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 file! Check the directory permission !); }/*** Obtain the 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 (the specified cache directory does not exist and creation failed! Check the directory permission !); 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 <div style = "color: red; ">. $ str. </div >;}}?> Demo. php: <? Php/** can be reproduced and used freely. Please retain the copyright information. Thank you for using it! * Class Name: Cache (For Php5) * Version: 1.0 * Description: dynamic Cache Class, used to control automatic page Cache generation, 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 Php5. I have not written the Php4 version yet. If you need it, please refer to it for modification. (It's easy. Don't be so lazy, huh, huh !). 2. this version is UTF-8 encoded. If the website uses other encoding, convert it by yourself. In Windows, use NotePad to open and save it as a file, and select the corresponding encoding (generally ANSI ), in Linux, use the corresponding editing software or the iconv command line. 3. copy and paste the above 2nd entries. * thoughts on caching: *