php文件缓存类汇总
|
本文实例讲述了php的文件缓存类。分享给大家供大家参考。具体分析如下: 缓存类是我们开发应用中会常用使用到的功能,下面就来给大家整理几个php文件缓存类了,各个文件缓存类写法不同,但在性能上会有区别,有兴趣测试的朋友可测试一下这些缓存类。 例1 代码如下: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 = 20000; //缓存过期时间 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.".cache"; $val = serialize($val); @file_put_contents($file,$val) or $this->error(__line__,"fail to write in file"); @chmod($file,0777); @touch($file,time()+$limit_time) or $this->error(__line__,"fail to change time"); } //读取缓存 function __get($key){ return $this->_get($key); } function _get($key){ $file = $this->cache_dir."/".$key.".cache"; if (@filemtime($file)>=time()){ return unserialize(file_get_contents($file)); }else{ @unlink($file) or $this->error(__line__,"fail to unlink"); return false; } } //删除缓存文件 function __unset($key){ return $this->_unset($key); } function _unset($key){ if (@unlink($this->cache_dir."/".$key.".cache")){ return true; }else{ return false; } } //检查缓存是否存在,过期则认为不存在 function __isset($key){ return $this->_isset($key); } function _isset($key){ $file = $this->cache_dir."/".$key.".cache"; 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) //清除所有缓存文件 function clear_all(){ $files = scandir($this->cache_dir); foreach ($files as $val){ @unlink($this->cache_dir."/".$val); } } function error($msg,$debug = false) { "; if($debug == true) { file_put_contents(date('Y-m-d H_i_s').".log",$str); return $str; }else{ die($str); } } } ?> 例2.从CI社区的stblog和CI的file_helper类中提取出来的php文件缓存类,一个简单的基于文件的key->value缓存类。 这个类可以用来缓存一些基本信息,比如博客的header,footer,sidebar中的一些不经常变化,从数据库中取出的内容,取数据前先判断文件缓存中的内容是否过期,如果没过期取出来,过期了则连接数据库查询,并将结果重新写入文件缓存,更新过期时间。跟memcache使用类似,不过更方便。用在一些小的应用上足够了. 具体代码如下 代码如下:/**
/**
/**
$file = $this->_file($key); return $this->write_file($file,$value); /**
/* 文件不存在或目录不可写 / /* 缓存没有过期,仍然可用 / $data = $this->read_file($file); if(FALSE !== $data) return FALSE; /* 缓存过期,删除之 / function read_file($file) if (function_exists('file_get_contents')) flock($fp,LOCK_SH);//读取之前加上共享锁 $data = ''; function write_file($path,$data,$mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) flock($fp,LOCK_EX); return TRUE; // For windows servers and safe_mode "on" installations we'll actually if (($fp = @fopen($file,FOPEN_WRITE_CREATE)) === FALSE) fclose($fp); fclose($fp); 例3.自己觉得很好用的php文件缓存 /**
/**
//缓存文件不存在 $file = $instance->_file($id); $data = $instance->_fileGetContents($file); if($data['expire'] == 0 || time() < $data['expire']) /**
$time = time(); $file = $instance->_file($id); return $instance->_filePutContents($file,$cache); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
