一个简单至极的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 相关文章推荐
一个php作的文本留言本的例子(二)
Oct 09 PHP
php面向对象全攻略 (七) 继承性
Sep 30 PHP
php下使用SimpleXML 处理XML 文件
Feb 27 PHP
php小经验:解析preg_match与preg_match_all 函数
Jun 29 PHP
让codeigniter与swfupload整合的最佳解决方案
Jun 12 PHP
php中JSON的使用与转换
Jan 14 PHP
php压缩和解压缩字符串的方法
Mar 14 PHP
PHP MVC框架路由学习笔记
Mar 02 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
Feb 17 PHP
php基于SQLite实现的分页功能示例
Jun 21 PHP
php的无刷新操作实现方法分析
Feb 28 PHP
php实现商城购物车的思路和源码分析
Jul 23 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
咖啡豆要不要放冰箱的原因
2021/03/04 冲泡冲煮
如何在PHP中使用Oracle数据库(2)
2006/10/09 PHP
php中通用的excel导出方法实例
2017/12/30 PHP
php利用ob_start()清除输出和选择性输出的方法
2018/01/18 PHP
ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
2019/04/02 PHP
PHP的静态方法与普通方法用法实例分析
2019/09/26 PHP
PHP中echo与print区别点整理
2021/03/09 PHP
一个很酷的拖动层的js类,兼容IE及Firefox
2009/06/23 Javascript
jQuery 动画弹出窗体支持多种展现方式
2010/04/29 Javascript
基于jquery的分页控件(C#)
2011/01/06 Javascript
JS Range HTML文档/文字内容选中、库及应用介绍
2011/05/12 Javascript
JavaScript实现QueryString获取GET参数的方法
2013/07/02 Javascript
JS实现网站菜单拖拽移位效果的方法
2015/09/24 Javascript
在AngularJS中使用jQuery的zTree插件的方法
2016/04/21 Javascript
easyui 中的datagrid跨页勾选问题的实现方法
2017/01/18 Javascript
vue指令以及dom操作详解
2017/03/04 Javascript
JS仿JQuery选择器功能
2017/03/08 Javascript
Vue.js仿微信聊天窗口展示组件功能
2017/08/11 Javascript
基于vue-cli创建的项目的目录结构及说明介绍
2017/11/23 Javascript
VUE脚手架具体使用方法
2019/05/20 Javascript
Vue CLI项目 axios模块前后端交互的使用(类似ajax提交)
2019/09/01 Javascript
nodejs+koa2 实现模仿springMVC框架
2020/10/21 NodeJs
JS如何监听div的resize事件详解
2020/12/03 Javascript
基于python的字节编译详解
2017/09/20 Python
pandas 小数位数 精度的处理方法
2018/06/09 Python
Python迭代器模块itertools使用原理解析
2019/12/11 Python
Python的pygame安装教程详解
2020/02/10 Python
Python爬虫爬取、解析数据操作示例
2020/03/27 Python
用python实现一个简单计算器(完整DEMO)
2020/10/14 Python
python opencv肤色检测的实现示例
2020/12/21 Python
荷兰的时尚市场:To Be Dressed
2019/05/06 全球购物
华为的Java面试题
2014/03/07 面试题
护士求职推荐信范文
2013/11/23 职场文书
公务员平时考核实施方案
2014/03/11 职场文书
入党积极分子学习优秀共产党员先进事迹思想汇报
2014/09/13 职场文书
审计局2014法制宣传日活动总结
2014/11/01 职场文书