php缓存类,PHP缓存类

// +----------------------------------------------------------------------

// |缓存类

// +----------------------------------------------------------------------

// | Author: justmepzy(justmepzy@gmail.com)

// +----------------------------------------------------------------------

class Cache{

//提示信息

public $tip_message;

//缓存目录

protected $cache_dir;

//缓存文件名

private $cache_file_name;

//缓存文件后缀

private $cache_file_suffix;

public function __construct($dir,$cache_file_suffix ='.php'){

$this->cache_dir = isset($dir)?$dir:dirname(__FILE__).DIRECTORY_SEPARATOR.'default_cache_data';

$this->cache_file_suffix =$cache_file_suffix;

if(!$this->dir_isvalid($this->cache_dir)){

die($this->tip_message);//创建目录失败

}

}

// +----------------------------------------------------------------------

// |添加一个值,如果已经存在,则返回false,写入文件失败返回false

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function add($cache_key,$cache_value,$life_time=1800){

if(file_exists($this->get_cache_file_name($cache_key))){

$this->tip_message ='缓存数据已存在.';

return false;

}

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

//以JSON格式写入文件

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='写入缓存失败.';

return false;

}

}

// +----------------------------------------------------------------------

// |添加一个值,如果已经存在,则覆写

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function set($cache_key,$cache_value,$life_time=1800){

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='写入缓存失败.';

return false;

}

}

// +----------------------------------------------------------------------

// |获取一个key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function get($cache_key){

if(!file_exists($this->get_cache_file_name($cache_key))){

return false;

}

$data =$this->object_to_array(json_decode(file_get_contents($this->get_cache_file_name($cache_key))));

if($this->check_isvalid($data['life_time'])){

unset($data['life_time']);

return $data['data'];

}else{

unlink($this->cache_file_name);

$this->tip_message ='数据已过期.';

return false;

}

}

// +----------------------------------------------------------------------

// |删除一个key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function delete($cache_key){

if(file_exists($this->get_cache_file_name($cache_key))){

if(unlink($this->get_cache_file_name($cache_key)))

return true;

else

return false;

}else{

$this->tip_message ='文件不存在.';

return true;

}

}

// +----------------------------------------------------------------------

// |清除所有缓存文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function flush(){

$this->delete_file($this->cache_dir);

}

// +----------------------------------------------------------------------

// |自动清除过期文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function auto_delete_expired_file(){

$this->delete_file($this->cache_dir,false);

}

// +----------------------------------------------------------------------

// |检查目录是否存在,不存在则创建

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function dir_isvalid($dir){

if (is_dir($dir))

return true;

try {

mkdir($dir,0777);

}catch (Exception$e) {

$this->tip_message ='所设定缓存目录不存在并且创建失败!请检查目录权限!';

return false;

}

return true;

}

// +----------------------------------------------------------------------

// |检查有效时间

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function check_isvalid($expired_time = 0) {

//if(!file_exists($this->cache_file_name)) return false;

if (!(@$mtime =filemtime($this->cache_file_name)))return false;

if (time() -$mtime >$expired_time)return false;

return true;

}

// +----------------------------------------------------------------------

// |获得缓存文件名

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function get_cache_file_name($key){

$this->cache_file_name =$this->cache_dir.DIRECTORY_SEPARATOR.md5($key).$this->cache_file_suffix;

return $this->cache_file_name;

}

// +----------------------------------------------------------------------

// |object对象转换为数组

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

protected function object_to_array($obj){

$_arr =is_object($obj) ? get_object_vars($obj) :$obj;

foreach ($_arr as $key =>$val) {

$val = (is_array($val) ||is_object($val)) ? object_to_array($val) :$val;

$arr[$key] =$val;

}

return $arr;

}

// +----------------------------------------------------------------------

// |删除目录下的所有文件

// +----------------------------------------------------------------------

// | $mode true删除所有 false删除过期

// +----------------------------------------------------------------------

protected function delete_file($dir,$mode=true) {

$dh=opendir($dir);

while ($file=readdir($dh)) {

if($file!="." &&$file!="..") {

$fullpath=$dir."/".$file;

if(!is_dir($fullpath)) {

if($mode){

unlink($fullpath);

}else{

$this->cache_file_name =$fullpath;

if(!$this->get_isvalid_by_path($fullpath)) unlink($fullpath);

}

}else {

delete_file($fullpath,$mode);

}

}

}

closedir($dh);

}

private function get_isvalid_by_path($path){

$data =$this->object_to_array(json_decode(file_get_contents($path)));

return $this->check_isvalid($data['life_time']);

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值