/** 通用緩存 說明: 根據傳遞參數的個數用來確認需要進行的操作 若傳遞2個參數,作用是判斷緩存檔是否已經過期 若傳遞3個參數,作用是對內容進行緩存 返回: 若傳遞2個參數: 若未超出緩
通用緩存
說明:
根據傳遞參數的個數用來確認需要進行的操作
若傳遞2個參數,作用是判斷緩存檔是否已經過期
若傳遞3個參數,作用是對內容進行緩存
返回:
若傳遞2個參數:
若未超出緩存期,則返回緩存的內容(該內容已解密)
若超出了緩存期,則返回bool值的FALSE
若傳遞3個參數:
對內容進行緩存(傳遞進來的內容未被加密,存儲前要先加密),無返回值
參數:
第1個參數:string型,緩存檔
第2個參數:int型,緩存多少秒
第3個參數:string型,緩存什麽[可選]
用法:
$che[0]=Run.'_cache/_qian_main_0.che'; //緩存檔
$che[1]=600; //緩存多少秒
$che[2]=Fun::Cache($che[0],$che[1]);
if(!is_bool($che[2])){ //未超出緩存期
echo '<pre>',var_dump('讀'),'</pre>';
echo $che[2];
unset($che);
}else{ //已超出緩存期
echo '<pre>',var_dump('寫'),'</pre>';
$che[2]='???';
Fun::Cache($che[0],$che[1],$che[2]);
echo $che[2];
unset($che);
}
//刪除緩存檔
Fun::Cache(Run.'_super_main_0.che',0,'');
/**/
1. [代码][PHP]代码
/**
通用緩存
說明:
根據傳遞參數的個數用來確認需要進行的操作
若傳遞2個參數,作用是判斷緩存檔是否已經過期
若傳遞3個參數,作用是對內容進行緩存
返回:
若傳遞2個參數:
若未超出緩存期,則返回緩存的內容(該內容已解密)
若超出了緩存期,則返回bool值的FALSE
若傳遞3個參數:
對內容進行緩存(傳遞進來的內容未被加密,存儲前要先加密),無返回值
參數:
第1個參數:string型,緩存檔
第2個參數:int型,緩存多少秒
第3個參數:string型,緩存什麽[可選]
用法:
$che[0]=Run.'_cache/_qian_main_0.che'; //緩存檔
$che[1]=600; //緩存多少秒
$che[2]=Fun::Cache($che[0],$che[1]);
if(!is_bool($che[2])){ //未超出緩存期
echo '<pre>',var_dump('讀'),'</pre>';
echo $che[2];
unset($che);
}else{ //已超出緩存期
echo '<pre>',var_dump('寫'),'</pre>';
$che[2]='???';
Fun::Cache($che[0],$che[1],$che[2]);
echo $che[2];
unset($che);
}
//刪除緩存檔
Fun::Cache(Run.'_super_main_0.che',0,'');
/**/
public static function Cache(){
$o=func_get_args();
if(!is_array($o)){
throw new exception('Error:'.__LINE__.',必須傳遞參數!');die();
}
$count=count($o);
switch($count){
case 2: //判斷緩存是否已經過期(未過期則返回緩存的內容,已過期則返回FALSE)
//緩存檔是否存在
if(!file_exists($o[0])){unset($o,$count);return FALSE;}
//取得上次的修改時間
$o[3]=filemtime($o[0]);
$o[3]=(!is_numeric($o[3]) or $o[3]<1) ? 0 : $o[3];
//緩存期
if(($_ENV['now']-$o[3])>=$o[1]){unset($o,$count);return FALSE;}
//返回解密之後的內容
unset($count);
return base64_decode(file_get_contents($o[0]));
case 3: //對內容進行緩存
//如果緩存時間小於1,則刪除緩存檔
if($o[1]<1){
if(file_exists($o[0])){@unlink($o[0]);}
unset($o,$count);
return ;
}
//更新緩存
file_put_contents($o[0],base64_encode('<!--cache start-->'.$o[2].'<!--cache end-->'),LOCK_EX);
if(file_exists($o[0])){
chmod($o[0],0777);
}
unset($o,$count);
return ;
default:
unset($o,$count);
throw new exception('Error:'.__LINE__.',參數個數不對!');die();
}
}
