一个简单至极的PHP缓存类代码


Posted in PHP onOctober 23, 2015

网上关于 PHP 缓存类的资料很多,不过这个类应该是我见过功能满足需求,但又无比简洁的一个。废话不多说,直接看代码吧!
使用说明:
1、实例化
$cache = new Cache();
2、设置缓存时间和缓存目录
$cache = new Cache(60, '/any_other_path/');
第一个参数是缓存秒数,第二个参数是缓存路径,根据需要配置。
默认情况下,缓存时间是 3600 秒,缓存目录是 cache/
3、读取缓存
$value = $cache->get('data_key');
4、写入缓存
$value = $cache->put('data_key', 'data_value');
完整实例:

$cache = new Cache(); 
 
//从缓存从读取键值 $key 的数据 
$values = $cache->get($key); 
 
//如果没有缓存数据 
if ($values == false) { 
//insert code here... 
//写入键值 $key 的数据 
$cache->put($key, $values); 
} else { 
//insert code here... 
}

Cache.class.php

<?php 
class Cache { 
private $cache_path;//path for the cache 
private $cache_expire;//seconds that the cache expires 
 
//cache constructor, optional expiring time and cache path 
public function Cache($exp_time=3600,$path="cache/"){ 
$this->cache_expire=$exp_time; 
$this->cache_path=$path; 
} 
 
//returns the filename for the cache 
private function fileName($key){ 
return $this->cache_path.md5($key); 
} 
 
//creates new cache files with the given data, $key== name of the cache, data the info/values to store 
public function put($key, $data){ 
$values = serialize($data); 
$filename = $this->fileName($key); 
$file = fopen($filename, 'w'); 
if ($file){//able to create the file 
fwrite($file, $values); 
fclose($file); 
} 
else return false; 
} 
 
//returns cache for the given key 
public function get($key){ 
$filename = $this->fileName($key); 
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache 
return false; 
} 
if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired 
$file = fopen($filename, "r");// read data file 
if ($file){//able to open the file 
$data = fread($file, filesize($filename)); 
fclose($file); 
return unserialize($data);//return the values 
} 
else return false; 
} 
else return false;//was expired you need to create new 
} 
} 
?>

相信大家一定会喜欢这个简洁的php缓存类代码,希望对大家的学习有所帮助。

PHP 相关文章推荐
删除无限级目录与文件代码共享
Jul 12 PHP
无数据库的详细域名查询程序PHP版(1)
Oct 09 PHP
PHP中$_SERVER的详细参数与说明
Jul 29 PHP
PHP pathinfo()获得文件的路径、名称等信息说明
Sep 13 PHP
PHP CodeBase:将时间显示为&quot;刚刚&quot;&quot;n分钟/小时前&quot;的方法详解
Jun 06 PHP
PHP防止post重复提交数据的简单例子
Jun 07 PHP
php生成shtml类用法实例
Dec 09 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
Dec 11 PHP
Symfony的安装和配置方法
Mar 17 PHP
微信支付开发维权通知实例
Jul 12 PHP
php实现二叉树中和为某一值的路径方法
Oct 14 PHP
PDO::commit讲解
Jan 27 PHP
10款实用的PHP开源工具
Oct 23 #PHP
PHP制作用户注册系统
Oct 23 #PHP
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
Oct 23 #PHP
PHP中文竖排转换实现方法
Oct 23 #PHP
浅谈php7的重大新特性
Oct 23 #PHP
php数字每三位加逗号的功能函数
Oct 22 #PHP
jQuery+PHP发布的内容进行无刷新分页(Fckeditor)
Oct 22 #PHP
You might like
CentOS下PHP安装Oracle扩展
2015/02/15 PHP
laravel-admin的多级联动方法
2019/09/30 PHP
Jquery 监视按键,按下回车键触发某方法的实现代码
2014/05/11 Javascript
Javascript验证方法大全
2015/09/21 Javascript
谈谈基于iframe、FormData、FileReader三种无刷新上传文件的方法
2015/12/03 Javascript
JavaScript模块化开发之SeaJS
2015/12/13 Javascript
JavaScript 函数的定义-调用、注意事项
2017/04/16 Javascript
JS解决position:sticky的兼容性问题的方法
2017/10/17 Javascript
JS动画实现回调地狱promise的实例代码详解
2018/11/08 Javascript
vue.js实现数据库的JSON数据输出渲染到html页面功能示例
2019/08/03 Javascript
详解JavaScript数据类型和判断方法
2020/09/04 Javascript
微信小程序实现点击生成随机验证码
2020/09/09 Javascript
[02:51]2014DOTA2 TI小组赛总结中国军团全部进军钥匙球馆
2014/07/15 DOTA
[01:55]《走出家门看比赛》——DOTA2 2015国际邀请赛同城线下观战
2015/07/18 DOTA
精确查找PHP WEBSHELL木马的方法(1)
2011/04/12 Python
python并发编程之多进程、多线程、异步和协程详解
2016/10/28 Python
Python数据可视化教程之Matplotlib实现各种图表实例
2019/01/13 Python
Python面向对象进阶学习
2019/05/21 Python
Python登录系统界面实现详解
2019/06/25 Python
基于Python实现大文件分割和命名脚本过程解析
2019/09/29 Python
Flask中endpoint的理解(小结)
2019/12/11 Python
python sklearn包——混淆矩阵、分类报告等自动生成方式
2020/02/28 Python
numpy 矩阵形状调整:拉伸、变成一位数组的实例
2020/06/18 Python
Python通过类的组合模拟街道红绿灯
2020/09/16 Python
python 利用matplotlib在3D空间中绘制平面的案例
2021/02/06 Python
Python接口自动化系列之unittest结合ddt的使用教程详解
2021/02/23 Python
HTML5 WebGL 实现民航客机飞行监控系统
2019/07/25 HTML / CSS
埃弗顿足球俱乐部官方网上商店:Everton Direct
2018/01/13 全球购物
渗透攻击的测试步骤
2014/06/07 面试题
千元咖啡店的创业计划书范文
2013/12/29 职场文书
小学英语教学反思案例
2014/02/04 职场文书
优良学风班总结材料
2014/02/08 职场文书
行政办公室岗位职责
2014/03/18 职场文书
2014年社区重阳节活动策划方案
2014/09/16 职场文书
Python异常类型以及处理方法汇总
2021/06/05 Python
详解Python函数print用法
2021/06/18 Python