php操作缓存
生成
获取
删除
1 class file{ 2 private $_dir; 3 const ext='.txt'; 4 public function __consruct(){ 5 $this->_dir dirname(__file___).'files/'; 6 } 7 public function cachedata($key,value='',$path=''){ 8 $filename = $this->_dir.$path.$key.self::ext; 9 if($value!==''){//将value写入缓存//删除缓存 if(is_null($value)){return @unlike($filename) }10 $dir=dirname($filename);11 if(!is_dir($dir)){12 mkdir($dir,0777);13 }14 return file_put_contents($filename,jsonencode($value));15 } if(!is_file($filename)){ return false; }else{ returnjson_decode(file_get_content($filename),true); }16 }24 25 } 26 27 28 //生成缓存29 $file= new firl();30 if($file->cachedata('index_mk_cache',$data)){31 32 echo success;33 }else(34 echo false;35 )36 //获取缓存
$file= new firl(); if($file->cachedata('index_mk_cache')){ var_dump($file->cahcedata('index_mk_cache'));exit; echo success; }else( echo false; )//删除缓存
37