PHP文件缓存类

<?php
/* 
 
*/ 
$fzz = new fzz_cache;
$fzz->kk = $_SERVER; //写入缓存
//$fzz->set("kk",$_SERVER,10000); //此方法不与类属性想冲突,可以用任意缓存名;
print_r($fzz->kk);  //读取缓存
//print_r($fzz->get("kk"));
//unset($fzz->kk); //删除缓存
//$fzz->_unset("kk");
var_dump(isset($fzz->kk)); //判断缓存是否存在
//$fzz->_isset("kk");
//$fzz->clear(); //清理过期缓存
//$fzz->clear_all(); //清理所有缓存文件
class fzz_cache{
 public $limit_time = 20; //缓存过期时间
 public $cache_dir = "data"; //缓存文件保存目录
 //写入缓存
 function __set($key , $val){
  $this->set($key ,$val);
 }
 //第三个参数为过期时间
 function set($key ,$val,$limit_time=null){
  $limit_time = $limit_time ? $limit_time : $this->limit_time;
  $file = $this->cache_dir."/".$key.".php";
  $val = serialize($val);
  @file_put_contents($file,$val) or $this->error(__line__,"文件写入失败");
  @chmod($file,0777)  or $this->error(__line__,"设定文件权限失败");
  @touch($file,time()+$limit_time) or $this->error(__line__,"更改文件时间失败");
 }
 //读取缓存
 function __get($key){
  return $this->get($key);
 }
 function get($key){
  $file = $this->cache_dir."/".$key.".php";
  if (@filemtime($file)>=time()){
   return unserialize(file_get_contents($file));
  }else{
   @unlink($file) ;
  }
 }
 //删除缓存文件
 function __unset($key){
  return $this->_unset($key);
 }
 function _unset($key){
  if (@unlink($this->cache_dir."/".$key.".php")){
   return true;
  }else{
   return false;
  }
 }
 //检查缓存是否存在,过期则认为不存在
 function __isset($key){
  return $this->_isset($key);
 }
 function _isset($key){
  $file = $this->cache_dir."/".$key.".php";
  if (@filemtime($file)>=time()){
   return true;
  }else{
   @unlink($file) ;
   return false;
  }
 }
 //清除过期缓存文件
 function clear(){
  $files = scandir($this->cache_dir);
  foreach ($files as $val){
   if (filemtime($this->cache_dir."/".$val)cache_dir."/".$val);
   }
  }
 }
 //清除所有缓存文件
 function clear_all(){
  $files = scandir($this->cache_dir);
  foreach ($files as $val){
   @unlink($this->cache_dir."/".$val);
  }
 }
 function error($line,$msg){
  die("出错文件:".__file__."\n出错行:$line\n错误信息:$msg");
 }
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值