Rumah >pembangunan bahagian belakang >tutorial php >php缓存技术 静态缓存 memcache缓存 redis缓存
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{ return json_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