1:獲取當(dāng)前目錄:
?dirname(__FILE__);
2:生成緩存文件:
$dir= dirname($filename); ? ? ? ? ? ?//獲取目錄
if(!is_dir($dir)){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷目錄是否存在
mkdir($dir,0777); ? ? ? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建目錄
}
3:刪除緩存文件:
@unlink($filename)
4:獲取緩存文件
file_get_contents($filename)
源碼
classFile{
private$_dir;
constEXT='.txt';
public function_construct(){
$this->_dir= dirname(__FILE__) .'/files/';
echo$this->_dir;
}
public functioncacheData($key,$value='',$path=''){
$filename=$this->_dir.$path.$key.self::EXT;
if($value!==''){//將value值寫入緩存
if(is_null($value)){
return@unlink($filename);
}
$dir= dirname($filename);
if(!is_dir($dir)){
mkdir($dir,0777);
}
returnfile_put_contents($filename,json_encode($value));
}
if(!is_file($filename)){
return FALSE;
}else{
returnjson_decode(file_get_contents($filename),true);
}
}
}