- Class DataCache
- {
- /**
- * Array conversion
- *
- * @ Param array $ array
- * @ Param string $ arrayName
- * @ Param array $ level
- *
- * @ Return string
- */
- Private function arrayEval ($ array, $ arrayName = '', $ level = 0)
- {
- $ Space = str_repeat ("t", $ level );
-
- If (emptyempty ($ arrayName ))
- {
- $ Evaluate = "arrayn $ space (n ";
- }
- Else
- {
- $ Evaluate = "$ {$ arrayName} = arrayn $ space (n ";
- }
-
- $ Space2 = str_repeat ("t", $ level + 1 );
- $ Comma = $ space2;
- If (! Emptyempty ($ array ))
- {
- Foreach ($ array as $ key => $ val)
- {
- $ Key = is_string ($ key )? '''. Addcslashes ($ key, ''\ '). ''': $ key;
- $ Val =! Is_array ($ val )&&(! Preg_match ('/^ -? [1-9] d * $/', $ val) | strlen ($ val)> 12 )? '''. Addcslashes ($ val, ''\ '). ''': $ val;
- If (is_array ($ val ))
- {
- $ Evaluate. = "$ comma $ key =>". arrayEval ($ val, '', $ level + 1 );
- }
- Else
- {
- $ Evaluate. = "$ comma $ key => $ val ";
- }
- $ Comma = ", n $ space2 ";
- }
- }
- $ Evaluate. = "n $ space )";
-
- // A ";" is required at the end.
- If ($ level = 0)
- {
- $ Evaluate. = ";";
- }
- Return $ evaluate;
- }
-
- /**
- * Write cache
- *
- * @ Param string $ path
- * @ Param string $ arrayName
- * @ Param array $ data
- *
- * @ Return boolean
- */
- Public static function writeCache ($ path, $ arrayName, $ data)
- {
- If ($ handle = fopen ($ path, 'W + '))
- {
- $ Data = self: arrayEval ($ data, $ arrayName );
-
- $ DataConvert ="
- Flock ($ handle, LOCK_EX );
- $ Rs = fputs ($ handle, $ dataConvert );
- Flock ($ handle, LOCK_UN );
- Fclose ($ handle );
- If ($ rs! = False)
- {
- Return true;
- }
- }
- Return false;
- }
- }
- ?>
Call method:
- /**
- * Generate file cache
- *
- * @ Param string $ filePath: Path to save the cached file
- * @ Param string $ array name in the cache file
- * @ Param array $ data
- *
- * @ Return boolean
- */
- DataCache: writeCache ($ filePath, $ arrayName, $ data );
Memcache to cache data. the file cache class:
- /**
- * File cache class
- * File caching
- */
- Class Cache_FileCache {
-
- /**
- * Set cache
- * @ Param $ key the key cached by the key
- * @ Param $ data cached content
- * @ Param $ cacheLife cache time (unit: Seconds) if it is 0, it indicates unlimited time
- * @ Return Bool
- */
- Public static function setCache ($ key, $ data, $ cacheLife)
- {
- If (file_exists (_ SITE_FILE_CACHE ))
- {
- @ $ File = _ SITE_FILE_CACHE. "/". $ key. ". php ";
- $ Cache = array ();
- $ Time = _ SYS_TIME;
- $ Cache ['content'] = $ data;
- $ Cache ['expire '] = $ cacheLife = 0? 0: $ time + $ cacheLife;
- $ Cache ['mtime'] = $ time;
- $ Cache = serialize ($ cache );
- $ SetReslut = @ file_put_contents ($ file, $ cache) or self: error (_ line __, "file write error ");
- $ ChmodReslut = @ chmod ($ file, 0777) or self: error (_ line __, "failed to set file permissions ");
- If ($ setReslut & $ chmodReslut)
- {
- Return true;
- }
- Else
- {
- Return false;
- }
- }
- }
-
- /**
- * Get cached data
- * @ Param $ key the key cached by the key
- * @ Return array
- */
- Public static function getCache ($ key)
- {
- @ $ File = _ SITE_FILE_CACHE. "/". $ key. ". php ";
- If (file_exists ($ file ))
- {
- $ Data = @ file_get_contents ($ file );
- $ Data = unserialize ($ data );
- If ($ data ['expire '] = 0 | $ data ['expire']> _ SYS_TIME)
- {
- Return $ data ['content'];
- }
- Else
- {
- Unlink ($ file );
- Return array ();
- }
- }
- }
-
- /**
- * Deleting cached files
- * @ Param $ key cache $ key
- * @ Return Bool
- */
- Public static function delCache ($ key)
- {
- If (@ unlink (_ SITE_FILE_CACHE. "/". $ key. ". php "))
- {
- Return true;
- }
- Else
- {
- Return false;
- }
- }
-
- /**
- * Clear all cached files
- * @ Return Bool
- */
-
- Public static function clearAllCache ()
- {
- $ Files = scandir (_ SITE_FILE_CACHE );
- Foreach ($ files as $ val)
- {
- @ Unlink (_ SITE_FILE_CACHE. "/". $ val );
- }
- }
-
- /**
- * Error handling functions
- * @ Param $ line: number of rows
- * @ Param $ msg information
- */
- Public static function error ($ line, $ msg)
- {
- Die ("error file:". _ file _. "/n error line: $ line/n error message: $ msg ");
- }
- }
- ?>
|