php 文件缓存类
//文件缓存类
class FileCache {
private $cacheTime = 3600; //默认缓存时间 秒
private $cacheDir = './filecache'; //缓存绝对路径
private $md5 = true; //是否对键进行加密
private $suffix = ".php"; //设置文件后缀
public function __construct($config){
if( is_array( $config ) ){
foreach( $config as $key=>$val ){
$this->$key = $val;
}
}
}
//设置缓存
public function set($key,$val,$leftTime=null){
$key = $this->md5 ? md5($key) : $key;
$leftTime = $leftTime ? $leftTime : $this->cacheTime;
!file_exists($this->cacheDir) && mkdir($this->cacheDir,0777);
$file = $this->cacheDir.'/'.$key.$this->suffix;
$val = serialize($val);
@file_put_contents($file,$val) or $this->error(__line__,"文件写入失败");
@chmod($file,0777) or $this->error(__line__,"设定文件权限失败");
@touch($file,time()+$leftTime) or $this->error(__line__,"更改文件时间失败");
}
//得到缓存
public function get($key){
$this->clear();
if( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
$val = file_get_contents($file);
return unserialize($val);
}
return null;
}
//判断文件是否有效
public function _isset($key){
$key = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key.$this->suffix;
if( file_exists($file) ){
if( @filemtime($file) >= time() ){
return true;
}else{
@unlink($file);
return false;
}
}
return false;
}
//删除文件
public function _unset($key){
if( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
return @unlink($file);
}
return false;
}
//清除过期缓存文件
public function clear(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
if (@filemtime($this->cacheDir."/".$val) < time()){
@unlink($this->cacheDir."/".$val);
}
}
}
//清除所有缓存文件
public function clear_all(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
@unlink($this->cacheDir."/".$val);
}
}
private function error($line,$msg){
die("出错文件:".__file__."/n出错行:$line/n错误信息:$msg");
}
调用方法:
//cacheTime 缓存时间 1个小时
$cacheFile = new FileCache(array('cacheTime'=>3600,'suffix'=>'.php'));
$cache_1 =$cacheFile->get('key1');
$cache_2 =$cacheFile->get('key2');
if( $cache_1 ){
$resArr_result = $cache_1;
}else{
//读数据库$resArr_result = array(***);
//设置缓存
$cacheFile->set('key1',$resArr_result);
}
php 文件缓存类的更多相关文章
- PHP文件缓存类
<?php /** * @desc 文件缓存 */ class Cache{ const C_FILE = '/Runtime/'; private $dir = ''; const EXT = ...
- 一个不错的PHP文件页面缓存类
在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 页面缓存类 <?php /* * 缓存类 cac ...
- php 文件缓存
http://www.oschina.net/code/snippet_162279_6098 <?php class cache { private static $_instan ...
- php文件缓存
1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...
- android文件缓存,并SD卡创建目录未能解决和bitmap内存溢出解决
1.相关代码: 加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ...
- C#之文件缓存
写在开头 今天就放假了,照理说应该写今年的总结了,但是回头一看,很久没有写过技术类的文字了,还是先不吐槽了. 关于文件缓存 写了很多的代码,常常在写EXE(定时任务)或者写小站点(数据的使用和客户端调 ...
- php文件缓存方法总结
为大家分享很全的php文件缓存,供大家参考,具体内容如下 <?php class cache { private static $_instance = null; protected ...
- php文件缓存数据
最近在做微信的摇一摇跑马活动,实现原理是用户摇动手机,通过ajax往数据库写入数据(小马跑的步数),然后PC端用过ajax每一秒钟从数据库中调取一次数据(小马跑的步数),然后显示在PC屏幕上,这样就会 ...
- 很实用的php的缓存类文件示例
http://www.php.cn/php-weizijiaocheng-376603.html <?php /* * 缓存类 cache */ class cache { //缓存目录 var ...
随机推荐
- MQ是什么 RabbitMQ
一.rabbitMQ是什么: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. 学过websocket的来理解rabbitMQ应该是 ...
- Leetcode 39
//经典回溯法class Solution { public: vector<vector<int>> combinationSum(vector<int>& ...
- Dubbo原理简介、与Zookeeper整合利用
官方文档:http://dubbo.io/books/dubbo-user-book/ Dubbo的简单介绍 Dubbo是一个分布式服务框架,架构如图: 节点角色说明: Provider: 暴露服务的 ...
- 无法安装64位版本的office因为在您的pc
无法安装64位版本的office因为在您的pcWindows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Installer\Products\00 ...
- 用django发送异步邮件
太阳底下没有新鲜事,github是一个神奇的地方,你有什么想法,需求,点子.其实别人早就想到,而且也已经做到. 所以不要高估自己,有什么想法还是GITHUB一下,免得成了井底之娃. 这几天一直在研究p ...
- python高级编程之列表推导式
1. 一个简单的例子 在Python中,如果我们想修改列表中所有元素的值,可以使用 for 循环语句来实现. 例如,将一个列表中的每个元素都替换为它的平方: >>> L = [1, ...
- java 生成xml文件
这里也使用的是import org.w3c.dom.Document; 首先创建document对象,给该对象赋值,然后将document对象使用transformer的transformer转换方法 ...
- 201621123005《java程序设计》第五周学习总结
201621123005<Java程序设计>第五周实验总结 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.多态.inferface.has-a.Compar ...
- Linux:root下的文件-anaconda-ks.cfg详解
anaconda-ks.cfg详解 系统安装的时候生成的一个文件,通过这个文件可以修改成自动安装的脚本,用于自动安装同样配置的系统. 自动生成的启动文件anaconda# Kickstart file ...
- ubuntu16切换hosts软件安装
之前用ubuntu切换hosts以为没有软件可用,直接用cp来替换hosts文件,今天网上搜了一下发现一个软件和window上用的切hosts功能一样,而且可以支持linux,mac,windows. ...