2PHP cache class & lt ;? Php ** cache class * author: many Cainiao * Contact Email: kingerqATmsnDOTcom * creation Time: * instance: include (cache. php); $ cachenewcache (30); $ cache-& gt; cacheCheck 2PHP cache class
CacheCheck (); echo date ("Y-m-d H: I: s"); // $ cache-> sort ache ('MV _ moves. php '); $ cache-> caching (); */class cache {// cache directory var $ cacheRoot = ". /cache/"; // The cache update time in seconds. 0 indicates that no cache var $ cacheLimitTime = 0; // The cache file name var $ cacheFileName = ""; // cache extension var $ cacheFileExt = "php";/** constructor * int $ cacheLimitTime cache update time */function cache ($ cacheLimitTime) {if (intval ($ cacheLimitTime) $ this-> cacheLimitTime = $ cacheLimitT Ime; $ this-> cacheFileName = $ this-> getCacheFileName (); ob_start ();}/** check whether the cache file is within the set Update Time * returns: if the file content is returned within the update time, otherwise, it will fail */function cacheCheck () {if (file_exists ($ this-> cacheFileName )) {$ cTime = $ this-> getFileCreateTime ($ this-> cacheFileName); if ($ cTime + $ this-> cacheLimitTime> time ()) {echo file_get_contents ($ this-> cacheFileName); ob_end_flush (); exit ;}return false ;}/ ** cache file or Output static * string $ staticFileName static file name (including relative path) */function caching ($ staticFileName = "") {if ($ this-> cacheFileName) {$ cacheContent = ob_get_contents (); // echo $ cacheContent; ob_end_flush (); if ($ staticFileName) {$ this-> saveFile ($ staticFileName, $ cacheContent );} if ($ this-> cacheLimitTime) $ this-> saveFile ($ this-> cacheFileName, $ cacheContent );}} /** Clear cache file * string $ fileName specify the file name (including the function) or al L (all) * return: true is returned if the cleanup is successful, false is returned, and false is returned. */function compute ache ($ fileName = "all") {if ($ fileName! = "All") {$ fileName = $ this-> cacheRoot. strtoupper (md5 ($ fileName )). ". ". $ this-> cacheFileExt; if (file_exists ($ fileName) {return @ unlink ($ fileName);} else return false;} if (is_dir ($ this-> cacheRoot )) {if ($ dir = @ opendir ($ this-> cacheRoot) {while ($ file = @ readdir ($ dir) {$ check = is_dir ($ file ); if (! $ Check) @ unlink ($ this-> cacheRoot. $ file) ;}@ closedir ($ dir); return true ;}else {return false ;}} else {return false ;}} /** generate the cache file name based on the current dynamic file */function getCacheFileName () {return $ this-> cacheRoot. strtoupper (md5 ($ _ SERVER ["REQUEST_URI"]). ". ". $ this-> cacheFileExt;}/** cache file creation time * string $ fileName cache file name (including relative path) * return: file generation time in seconds, 0 */function getFileCreateTime ($ fileName) {if (! Trim ($ fileName) return 0; if (file_exists ($ fileName) {return intval (filemtime ($ fileName);} else return 0 ;} /** save the file * string $ fileName file name (including relative path) * string $ text file content * return: True is returned successfully, false is returned if a failure */function saveFile ($ fileName, $ text) {if (! $ FileName |! $ Text) return false; if ($ this-> makeDir (dirname ($ fileName) {if ($ fp = fopen ($ fileName, "w ")) {if (@ fwrite ($ fp, $ text) {fclose ($ fp); return true ;}else {fclose ($ fp); return false ;}}} return false;}/** create directory consecutively * string $ dir directory string * int $ mode permission number * return: true if the directory is created successfully or all has been created, otherwise, false */function makeDir ($ dir, $ mode = "0777") {if (! $ Dir) return 0; $ dir = str_replace ("\", "/", $ dir); $ mdir = ""; foreach (explode ("/", $ dir) as $ val) {$ mdir. = $ val. "/"; if ($ val = ".. "| $ val = ". "| trim ($ val) =" ") continue; if (! File_exists ($ mdir) {if (! @ Mkdir ($ mdir, $ mode) {return false ;}} return true ;}}?>