Php cache. inc. php: & lt ;? PhpclassCache {*** $ 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 * php Cache class
Cache. inc. php:
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 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: brea K;} 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 () after loading the cache, terminate the execution of the original page program. 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 "This is Cache. "; // 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 the cached 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''. $ Str .'
';}}?> Demo. php:
Load (); // load the cache. if the cache is valid, do not execute the following page code // the page code starts echo date ('H: I: s jS f '); // the page code ends $ cache-> write (); // when the code is run for the first time or the cache expires, the cache is generated ----------------------------------------------------------------- require_once. inc. php '); $ cachedir = '. /Cache/'; // Set the cache directory $ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used, $ cache = new Cache ($ cachedir); if ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite to update the cache, and so on. you can also set some other operations $ cache-> load (); // load the cache, if the cache is valid, do not execute the following page code // the page code starts $ content = date ('H: I: s jS f'); echo $ content; // page code ends $ cache-> write (1, $ content); // The cache is generated when the first running or cache expires ------------------------------------ Demo3 logs require_once ('cache. inc. php '); define ('cacheenable', true); if (CACHEENABLE) {$ cachedir = '. /Cache/'; // Set the cache directory $ Cache = new Cache ($ ca Chedir, 10); // The default setting is used for parameter omission. $ cache = new Cache ($ cachedir); if ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite to update the cache, and so on. you can also set some other operations $ cache-> load (); // load the cache, if the cache is valid, do not execute the following page code} // the page code starts $ content = date ('H: I: s jS f'); echo $ content; // when the page code ends, if (CACHEENABLE) $ cache-> write (1, $ content); // when the first operation or cache expires, the cache is generated */?>