define('CACHE_ROOT','./');
include_once('./cache.func.php');
$file = 'qzp';
$cacheFile = getCacheFileByID($file,'info/');
print_R(readCache($cacheFile));
$info = array(
'username'=>'qzp',
'area_name'=>'admin',
'mp_name'=>'1234',
'gh_name'=>'5678');
writeCache($cacheFile,$info);
cache.func.php文件
function arrayeval($array, $level = 0) {
$space = '';
for($i = 0; $i <= $level; $i++) {
$space .= "t";
}
$evaluate = "Arrayn$space(n";
$comma = $space;
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$space";
}
$evaluate .= "n$space)";
return $evaluate;
}
function getCacheFileByID($id,$pre='info/',$md5=true){
if($md5){
$md5id = md5($id);
return CACHE_ROOT.'/'.$pre.substr($md5id,0,2).'/'.substr($md5id,2,2).'/'.$id;
}else{
return CACHE_ROOT.'/'.$pre.$id;
}
}
function readCache($filename,$time=0){
if($datas = @file_get_contents($filename)){
$datas = unserialize($datas);
if($time < 1 || (time() - $datas['time'] < $time)){
return $datas['data'];
}
}
return false;
}
function writeCache($filename,$data){
makeDir(dirname($filename));
if($handle = fopen($filename,'w+')){
$datas = array('data'=>$data,'time'=>time());
flock($handle,LOCK_EX);
$rs = fputs($handle,serialize($datas));
flock($handle,LOCK_UN);
fclose($handle);
if($rs!==false){
return true;
}
}
return false;
}
function makeDir($path)
{
if (! file_exists ( $path )) {
makeDir ( dirname ( $path ) );
if (! mkdir ( $path, 0777 ))
die ( '无法创建文件夹' . $path );
}
}
|