Class Cache { Private static $ _ instance; Protected $ _ cacheId = null; Const CLEANING_MODE_ALL = 'all '; Const CLEANING_MODE_OLD = 'old '; Protected $ _ options = array ( 'Cache _ dir' => null, // data cache directory 'Life _ time' => 7200, // Cache time 'Page _ dir' => null, // text cache directory 'Cache _ prefix' => 'cache _ '// cache prefix ); Private function _ construct (){} // Create the _ clone method to prevent objects from being copied and cloned. Private function _ clone (){} /** * Get the cache object. If there is a direct return, if there is no instantiation itself * @ Return object cache */ Public static function getInstance (){ If (! Self: $ _ instance ){ Self: $ _ instance = new self (); } Return self: $ _ instance; } /** * Set cache Parameters * @ Param array $ options indicates the cache parameter set to be set. */ Public function setOptions ($ options = array ()){ While (list ($ name, $ value) = each ($ options )){ $ This-> setOption ($ name, $ value ); } } /** * Get the current cache parameter. If $ name is null, all parameters are returned. Otherwise, this parameter value is returned. * @ Param string $ name: name of the parameter to be returned * @ Return string or array $ option; */ Public function getOption ($ name = null ){ If (null ===$ name) Return $ this-> _ options; If (! Is_string ($ name )){ ThrowException ("Incorrect Parameter name: $ name "); } If (array_key_exists ($ name, $ this-> _ options )){ Return $ this-> _ options [$ name]; } } /** * Set cache Parameters * @ Param array $ options the cache parameter to be set */ Protected function setOption ($ name, $ value ){ If (! Is_string ($ name )){ ThrowException ("Incorrect Parameter name: $ name "); } $ Name = strtolower ($ name ); If (array_key_exists ($ name, $ this-> getOption ())){ $ This-> _ options [$ name] = $ value; } If ($ this-> _ options ['cache _ dir'] = null ){ $ This-> setOption ('cache _ dir', $ this-> getTmpDir (). DIRECTORY_SEPARATOR ); } If ($ this-> _ options ['page _ dir'] = null ){ $ This-> setOption ('page _ dir', $ this-> getTmpDir (). DIRECTORY_SEPARATOR ); } } /** * Read data cache. If it does not exist or expires, false is returned. * @ Param string $ id cache ID * @ Return false or data */ Public function load ($ id ){ $ This-> _ cacheId = $ id; $ File = $ this-> getOption ('cache _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; If (@ filemtime ($ file)> = time ()){ Return unserialize (file_get_contents ($ file )); } Else { @ Unlink ($ file ); Return false; } } /** * Save the data cache and set the cache expiration time * @ Param array or string $ data the data to be cached * @ Param int $ lifeTime cache expiration time */ Public function save ($ data, $ lifeTime = null ){ If (null! ==$ LifeTime) $ This-> setOption ('life _ time', $ lifeTime ); $ File = $ this-> getOption ('cache _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; $ Data = serialize ($ data ); @ File_put_contents ($ file, $ data ); @ Chmod ($ file, 0777 ); @ Touch ($ file, time () + $ this-> getOption ('life _ time']); } /** * Read the output cache. If the cache does not exist or expires, the output cache will be restarted. * @ Param string $ id cache ID */ Public function start ($ id ){ $ This-> _ cacheId = $ id; $ File = $ this-> getOption ('page _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; If (@ filemtime ($ file)> = time ()){ Return file_get_contents ($ file ); } Else { @ Unlink ($ file ); Ob_start (); Return false; } } /** * Delete the specified ID cache. * @ Param string $ id cache ID */ Public function remove ($ id ){ $ This-> _ cacheId = $ id; // Delete the data cache with the matching conditions $ File = $ this-> getOption ('cache _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; @ Unlink ($ file ); // Delete the output cache with the matching conditions $ File = $ this-> getOption ('page _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; @ Unlink ($ file ); } /** * Save the output cache and set the cache expiration time * @ Param int $ lifeTime cache expiration time */ Public function end ($ lifeTime = null ){ If (null! ==$ LifeTime) $ This-> setOption ('life _ time', $ lifeTime ); $ File = $ this-> getOption ('page _ dir'). $ this-> getOption ('cache _ prefix'). $ this-> _ cacheId; $ Data = ob_get_contents (); Ob_end_clean (); @ File_put_contents ($ file, $ data ); @ Chmod ($ file, 0777 ); @ Touch ($ file, time () + $ this-> getOption ('life _ time']); } /** * Clear the corresponding cache based on parameters * @ Param string $ mode cache type, including (CLEANING_MODE_ALL: All caches, CLEANING_MODE_OLD: expired caches) */ Public function clear ($ mode = CLEANING_MODE_OLD ){ $ Dirs = array ('cache _ dir', 'page _ dir '); Foreach ($ dirs as $ value ){ If (null! = $ This-> getOption ($ value )){ $ Files = scandir ($ this-> getOption ($ value )); Switch ($ mode ){ Case CLEANING_MODE_ALL: Default: Foreach ($ files as $ val ){ @ Unlink ($ this-> getOption ($ value). $ val ); } Break; Case CLEANING_MODE_OLD: Default: Foreach ($ files as $ val ){ If (filemtime ($ this-> getOption ($ value). $ val) <time ()){ @ Unlink ($ this-> getOption ($ value). $ val ); } } Break; } } } } /** * Use a temporary folder as the cache folder. * @ Return $ dir Temporary Folder path */ Public function getTmpDir (){ $ Tmpdir = array (); Foreach (array ($ _ ENV, $ _ SERVER) as $ tab ){ Foreach (array ('tmpdir', 'temp ', 'tmp', 'windir', 'systemroot') as $ key ){ If (isset ($ tab [$ key]) { If ($ key = 'windir') or ($ key = 'systemroot ')){ $ Dir = realpath ($ tab [$ key]. '\ temp '); } Else { $ Dir = realpath ($ tab [$ key]); } If ($ this-> _ isGoodTmpDir ($ dir )){ Return $ dir; } } } } $ Upload = ini_get ('upload _ tmp_dir '); If ($ upload ){ $ Dir = realpath ($ upload ); If ($ this-> _ isGoodTmpDir ($ dir )){ Return $ dir; } } If (function_exists ('sys _ get_temp_dir ')){ $ Dir = sys_get_temp_dir (); If ($ this-> _ isGoodTmpDir ($ dir )){ Return $ dir; } } // Detect by creating a temporary file $ TempFile = tempnam (md5 (uniqid (rand (), TRUE )),''); If ($ tempFile ){ $ Dir = realpath (dirname ($ tempFile )); Unlink ($ tempFile ); If ($ this-> _ isGoodTmpDir ($ dir )){ Return $ dir; } } If ($ this-> _ isGoodTmpDir ('/tmp ')){ Return '/tmp '; } If ($ this-> _ isGoodTmpDir ('\ temp ')){ Return '\ temp '; } Throw new Exception ('the temporary directory cannot be determined. Please manually specify cache_dir ', E_USER_ERROR ); } /** * Verify that the given temporary directory is readable and writable. * * @ Param string $ dir Temporary Folder path * @ Return boolean true or false whether the Temporary Folder path can be read and written */ Protected function _ isGoodTmpDir ($ dir ){ If (is_readable ($ dir )){ If (is_writable ($ dir )){ Return true; } } Return false; } } // Endclass
|