一个简单至极的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中使用与Perl兼容的正则表达式
Nov 26 PHP
PHP的简易冒泡法代码分享
Aug 28 PHP
解析php安全性问题中的:Null 字符问题
Jun 21 PHP
php时区转换转换函数
Jan 07 PHP
thinkphp在模型中自动完成session赋值示例代码
Sep 09 PHP
PHP简单实现断点续传下载的方法
Sep 25 PHP
thinkPHP实现将excel导入到数据库中的方法
Apr 22 PHP
支付宝服务窗API接口开发php版本
Jul 20 PHP
PHP简单计算两个时间差的方法示例
Jun 20 PHP
PHP处理bmp格式图片的方法分析
Jul 04 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
Apr 04 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入门教程 精简版
2009/12/13 PHP
通过PHP的内置函数,通过DES算法对数据加密和解密
2012/06/21 PHP
php将mysql数据库整库导出生成sql文件的具体实现
2014/01/08 PHP
php 发送带附件邮件示例
2014/01/23 PHP
php采用curl实现伪造IP来源的方法
2014/11/21 PHP
PHP设计模式之简单投诉页面实例
2016/02/24 PHP
URI、URL和URN之间的区别与联系
2006/12/20 Javascript
JQuery里选择超链接的实现代码
2011/05/22 Javascript
jquery动画1.加载指示器
2012/08/24 Javascript
实现动画效果核心方式的js代码
2013/09/27 Javascript
jquery实现类似淘宝星星评分功能有截图
2014/09/15 Javascript
JavaScript中的style.cssText使用教程
2014/11/06 Javascript
深入理解JavaScript系列(28):设计模式之工厂模式详解
2015/03/03 Javascript
javascript实现俄罗斯方块游戏的思路和方法
2015/04/27 Javascript
js表格排序实例分析(支持int,float,date,string四种数据类型)
2015/05/06 Javascript
非常漂亮的相册集 使用jquery制作相册集
2016/04/28 Javascript
nodejs个人博客开发第七步 后台登陆
2017/04/12 NodeJs
vue.js内部自定义指令与全局自定义指令的实现详解(利用directive)
2017/07/11 Javascript
JS的函数调用栈stack size的计算方法
2018/06/24 Javascript
React 实现车牌键盘的示例代码
2019/12/20 Javascript
Vue项目利用axios请求接口下载excel
2020/11/17 Vue.js
python通过ssh-powershell监控windows的方法
2015/06/02 Python
python 网络编程详解及简单实例
2017/04/25 Python
简单实现python数独游戏
2018/03/30 Python
python打印异常信息的两种实现方式
2019/12/24 Python
pytorch 常用线性函数详解
2020/01/15 Python
如何使用python切换hosts文件
2020/04/29 Python
python seaborn heatmap可视化相关性矩阵实例
2020/06/03 Python
python re.match()用法相关示例
2021/01/27 Python
详解html2canvas截图不能截取圆角图片的解决方案
2018/01/30 HTML / CSS
我想声明一个指针并为它分配一些空间, 但却不行。这些代码有什么 问题?char *p; *p = malloc(10);
2016/10/06 面试题
食堂厨师岗位职责
2014/08/25 职场文书
员工保密协议书
2014/09/27 职场文书
创业计划书之家教中心
2019/09/25 职场文书
详解Spring事件发布与监听机制
2021/06/30 Java/Android
使用scrapy实现增量式爬取方式
2022/06/21 Python