一个简单至极的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 相关文章推荐
几种显示数据的方法的比较
Oct 09 PHP
PHP音乐采集(部分代码)
Feb 14 PHP
Zend Studio 无法启动的问题解决方法
Dec 04 PHP
配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境
Apr 25 PHP
php数组去重复数据示例
Feb 25 PHP
服务器上配置PHP运行环境教程
Feb 12 PHP
windows平台中配置nginx+php环境
Dec 06 PHP
php+ajax注册实时验证功能
Jul 20 PHP
Redis使用Eval多个键值自增的操作实例
Nov 04 PHP
PHP编写daemon process 实例详解
Nov 13 PHP
thinkPHP内置字符串截取函数用法详解
Nov 15 PHP
自制PHP框架之模型与数据库
May 07 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
深入php数据采集的详解
2013/06/02 PHP
ThinkPHP实现支付宝接口功能实例
2014/12/02 PHP
人脸识别测颜值、测脸龄、测相似度微信接口
2016/04/07 PHP
Yii实现复选框批量操作实例代码
2017/03/15 PHP
PHP性能分析工具xhprof的安装使用与注意事项
2017/12/19 PHP
php分享朋友圈的实现代码
2019/02/18 PHP
javaScript - 如何引入js代码
2021/03/09 Javascript
使用node.js 获取客户端信息代码分享
2014/11/26 Javascript
详解webpack es6 to es5支持配置
2017/05/04 Javascript
ES6学习之变量的两种命名方法示例
2017/07/18 Javascript
微信小程序获取手机号授权用户登录功能
2017/11/09 Javascript
基于Vue实现微信小程序的图文编辑器
2018/07/25 Javascript
vue实现父子组件之间的通信以及兄弟组件的通信功能示例
2019/01/29 Javascript
vue实现div单选多选功能
2020/07/16 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
Vue中inheritAttrs的使用实例详解
2020/12/31 Vue.js
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
2014/06/09 Python
Python 关于反射和类的特殊成员方法
2017/09/14 Python
python分析作业提交情况
2017/11/22 Python
Django REST为文件属性输出完整URL的方法
2017/12/18 Python
Python实现识别手写数字大纲
2018/01/29 Python
Python的多维空数组赋值方法
2018/04/13 Python
Python实现确认字符串是否包含指定字符串的实例
2018/05/02 Python
python做接口测试的必要性
2019/11/20 Python
Pandas将列表(List)转换为数据框(Dataframe)
2020/04/24 Python
浅谈python处理json和redis hash的坑
2020/07/16 Python
解决python打开https出现certificate verify failed的问题
2020/09/03 Python
HTML5几个设计和修改的页面范例分享
2015/09/29 HTML / CSS
.NET remoting中对象激活的两种方式
2015/06/08 面试题
销售工作人员的自我评价分享
2013/11/10 职场文书
有针对性的求职自荐信
2013/11/14 职场文书
医药营销个人求职信
2014/04/12 职场文书
年检委托书
2014/08/30 职场文书
教师见习总结范文
2015/06/23 职场文书
公司晚会主持词
2019/04/17 职场文书
SpringAop日志找不到方法的处理
2021/06/21 Java/Android