- 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";
- }
- $space 2 = str_repeat ("T", $level + 1);
- $comma = $space 2;
- 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)";
- Finally, we need a ";"
- 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;
- }
- }
- ?>
Copy Code
Call Method:
- /**
- * Generate file Cache
- *
- * @param string $filePath save path to cache file
- * @param string $arrayName The name of the array stored in the cache file
- * @param array $data data
- *
- * @return Boolean
- */
- Datacache::writecache ($filePath, $arrayName, $data);
Copy Code
Memcache to cache the data, this file caches the class:
- /**
- * File Cache class
- * Provide file cache
- */
- Class cache_filecache{
- /**
- * Set Cache
- * @param $key Cached keyword key
- * @param $data Cached content
- * @param $cacheLife Cache time (in seconds) if 0 means 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__, "Set file permissions failed");
- if ($setReslut && $chmodReslut)
- {
- return true;
- }
- Else
- {
- return false;
- }
- }
- }
- /**
- * Get Cached data
- * @param $key Cached keyword 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 ();
- }
- }
- }
- /**
- * Delete cache 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 cache files
- * @return Bool
- */
- public static function Clearallcache ()
- {
- $files = Scandir (__site_file_cache);
- foreach ($files as $val)
- {
- @unlink (__site_file_cache. " /". $val);
- }
- }
- /**
- * Error handling function
- * Number of lines @param $line
- * @param $msg Information
- */
- public static function error ($line, $msg)
- {
- Die ("Error File:". __file__. " /N Error Line: $line/N error message: $msg ");
- }
- }
- ?>
Copy Code |