Class Cache { /** Cache directory **/ Var $ CacheDir = './C '; /** Cached files **/ Var $ CacheFile = ''; /** File cache time (minutes )**/ Var $ CacheTime = 0; /** Whether the file is cached **/ Var $ CacheFound = False; /** Error and debugging information **/ Var $ DebugMsg = NULL; Function Cache ($ CacheTime = 0 ){ $ This-> CacheTime = $ CacheTime; } Private function Run (){ /** The cache time is greater than 0. it detects the modification time of the cached file. it is the cache file name within the cache time. if the cache time exceeds the cache time, it is False, If the value is less than or equal to 0, false is returned and cached files are cleared. **/ Return $ this-> CacheTime? $ This-> CheckCacheFile (): $ this-> CleanCacheFile (); } Function GetCache ($ VistUrl, $ CacheFileType = 'html ') { $ This-> SetCacheFile ($ VistUrl, $ CacheFileType ); $ FileName = $ this-> CheckCacheFile (); If ($ fileName) { $ Fp = fopen ($ fileName, "r "); $ Content _ = fread ($ fp, filesize ($ fileName )); Fclose ($ fp ); Return $ content _; } Else { Return false; } } Private function SetCacheFile ($ VistUrl, $ CacheFileType = 'html '){ If (empty ($ VistUrl )){ /** Index.html **/ $ This-> CacheFile = 'index '; } Else { /** When the parameter is $ _ POST **/ $ This-> CacheFile = is_array ($ VistUrl )? Implode ('.', $ VistUrl): $ VistUrl; } $ This-> CacheFile = $ this-> CacheDir. '/'. md5 ($ this-> CacheFile ); $ This-> CacheFile. = '.'. $ CacheFileType; } Function SetCacheTime ($ t = 60 ){ $ This-> CacheTime = $ t; } Private function CheckCacheFile (){ If (! $ This-> CacheTime |! File_exists ($ this-> CacheFile) {Return False ;} /** Compare the time difference between the file creation/modification date and the current date **/ $ GetTime = (Time ()-Filemtime ($ this-> CacheFile)/(60*1 ); /** The Filemtime function has a cache. Be sure to clean it **/ Clearstatcache (); $ This-> Debug ('Time Limit '. ($ GetTime * 60).'/'. ($ this-> CacheTime * 60 ).''); $ This-> CacheFound = $ GetTime <= $ this-> CacheTime? $ This-> CacheFile: False; Return $ this-> CacheFound; } Function SaveToCacheFile ($ VistUrl, $ Content, $ CacheFileType = 'html '){ $ This-> SetCacheFile ($ VistUrl, $ CacheFileType ); If (! $ This-> CacheTime ){ Return False; } /** Check whether the cache directory exists **/ If (true ===$ this-> CheckCacheDir ()){ $ CacheFile = $ this-> CacheFile; $ CacheFile = str_replace ('//', '/', $ CacheFile ); $ Fp = @ fopen ($ CacheFile, "wb "); If (! $ Fp ){ $ This-> Debug ('open file'. $ CacheFile. 'fail '); } Else { If (@! Fwrite ($ fp, $ Content )){ $ This-> Debug ('write'. $ CacheFile. 'fail '); } Else { $ This-> Debug ('cached file '); }; @ Fclose ($ fp ); } } Else { /** The cache directory does not exist or cannot be created **/ $ This-> Debug ('cache folder'. $ this-> CacheDir. 'not found '); } } Private function CheckCacheDir (){ If (file_exists ($ this-> CacheDir) {Return true ;} /** Save the current working directory **/ $ Location = getcwd (); /** Divide the path into a single directory **/ $ Dir = split ("/", $ this-> CacheDir ); /** Create a directory cyclically **/ $ CatchErr = True; For ($ I = 0; $ I If (! File_exists ($ Dir [$ I]) { /** If a directory fails to be created, False is returned, and the return value of creating the last Directory is returned **/ $ CatchErr = @ mkdir ($ Dir [$ I], 0777 ); } @ Chdir ($ Dir [$ I]); } /** Switch to the original directory after creation **/ Chdir ($ Location ); If (! $ CatchErr ){ $ This-> Debug ('create folder'. $ this-> CacheDir. 'fail '); } Return $ CatchErr; } Private function CleanCacheFile (){ If (file_exists ($ this-> CacheFile )){ @ Chmod ($ this-> CacheFile, 777 ); @ Unlink ($ this-> CacheFile ); } /** Set no cached file **/ $ This-> CacheFound = False; Return $ this-> CacheFound; } Function Debug ($ msg = ''){ If (DEBUG ){ $ This-> DebugMsg [] = '[Cache]'. $ msg; } } Function GetError (){ Return empty ($ this-> DebugMsg )? '':" N ". implode (" N ", $ this-> DebugMsg ); } }/* End of class */ ?>
|