1. For the general variable, the variable into a PHP language format, written to the file, as long as the include this file is equivalent to loading the cache;
2. For the array-type variables, the array into the PHP language definition array string, written to the file, as long as the include is equivalent to loading the cache;
3. Cached cache time control, by getting the cache file creation time and current time to compare, if not to update the time, direct read cache, if the update time, query the database,
File Cache class:
The code is as follows |
Copy Code |
<?php 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 (empty ($arrayName)) { $evaluate = "Arrayn$space (n"); } Else { $evaluate = "${$arrayName} = Arrayn$space (n"; }
$space 2 = str_repeat ("T", $level + 1); $comma = $space 2; if (!empty ($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 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 = "<?PHPN". $data;
Flock ($handle, LOCK_EX); $rs = fputs ($handle, $dataConvert); Flock ($handle, lock_un); Fclose ($handle); if ($rs!== false) { return true; } } return false; }
} |
Call method
The code is as follows |
Copy Code |
/** * Generate file Cache * * @param string $filePath The save path to the cached 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); |
Memcache to cache data
Side to provide this file cache class, I hope you can look.
The code is as follows |
Copy Code |
<?php /** * File Cache class * Provide file caching * @author Guoyubin (263421949@qq.com) */ Class cache_filecache{
/** * Set Cache * @param $key-Cached keyword key * @param $data Cached content * @param $cacheLife Cache time (in seconds) if 0 indicates infinite 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 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 function * @param $line number of lines * @param $msg Information */ public static function error ($line, $msg) { Die ("Error file:. __file__.") /N Error Line: $line/N error message: $msg "); } } ?> |