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
php操作redis
1 安装phpredis扩展
2php连接redis服务-connection(127.0.0.1,6379)
3set设置缓存
4get 获取缓存 del删除缓存
<?php
$redis= new Redis();
$redis->connect('127.0.0.1',6379);
$redis->set('name','tom');
$redis->setex('name',12,'rose')
?>
<?php
$redis= new Redis();
$redis->connect('127.0.0.1',6379);
var_dump($redis->get('name'));
?>
php操作memcache
安装memcache扩展
连接服务-connection('memcache_host',11211);
set设置缓存
get获取缓存