一个简单至极的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
cmd下运行php脚本
Nov 25 PHP
PHP chmod 函数与批量修改文件目录权限
May 10 PHP
解密ThinkPHP3.1.2版本之模板继承
Jun 19 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
Aug 05 PHP
PHP SplObjectStorage使用实例
May 12 PHP
php 实现一个字符串加密解密的函数实例代码
Nov 01 PHP
php 数组元素快速去重
May 05 PHP
PHP基于自增数据如何生成不重复的随机数示例
May 19 PHP
PHP中引用类型和值类型功能与用法示例
Feb 26 PHP
Laravel 手动开关 Eloquent 修改器的操作方法
Dec 30 PHP
laravel5.6 框架邮件队列database驱动简单demo示例
Jan 26 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 mssql 日期出现中文字符的解决方法
2009/03/10 PHP
php 动态添加记录
2009/03/10 PHP
php下将图片以二进制存入mysql数据库中并显示的实现代码
2010/05/27 PHP
在Linux系统下一键重新安装WordPress的脚本示例
2015/06/30 PHP
DEDE实现转跳属性文档在模板上调用出转跳地址
2016/11/04 PHP
在Mac OS下搭建LNMP开发环境的步骤详解
2017/03/10 PHP
js的逻辑运算符 ||
2010/05/31 Javascript
jQuery+ajax实现鼠标单击修改内容的方法
2014/06/27 Javascript
AngularJS中处理多个promise的方式
2016/02/02 Javascript
微信小程序 loading(加载中提示框)实例
2016/10/28 Javascript
Vue.js实现价格计算器功能
2020/03/30 Javascript
react.js组件实现拖拽复制和可排序的示例代码
2018/08/20 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
2018/08/25 Javascript
js/jQuery实现全选效果
2019/06/17 jQuery
JavaScript Blob对象原理及用法详解
2020/10/14 Javascript
[44:50]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第二局
2016/02/26 DOTA
python相似模块用例
2016/03/04 Python
python实现验证码识别功能
2018/06/07 Python
利用Python读取txt文档的方法讲解
2018/06/23 Python
Python 通过requests实现腾讯新闻抓取爬虫的方法
2019/02/22 Python
python内置函数sorted()用法深入分析
2019/10/08 Python
Django URL参数Template反向解析
2020/11/24 Python
HTML5+CSS3网页加载进度条的实现,下载进度条的代码实例
2016/12/30 HTML / CSS
使用HTML5做的导航条详细步骤
2020/10/19 HTML / CSS
原装进口全世界:天猫国际
2016/08/03 全球购物
施华洛世奇韩国官网:SWAROVSKI韩国
2018/06/05 全球购物
全球最大的户外用品零售商之一:The House
2018/06/12 全球购物
Dogeared官网:在美国手工制作的珠宝
2019/08/24 全球购物
廉洁教育学习材料
2014/05/19 职场文书
数学教研活动总结
2014/07/02 职场文书
社区娱乐活动方案
2014/08/21 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python
漫画「古见同学有交流障碍症」第25卷封面公开
2022/03/21 日漫
动画「进击的巨人」第86话播出感谢绘公开
2022/03/21 日漫
一文简单了解MySQL前缀索引
2022/04/03 MySQL
微信小程序调用python模型
2022/04/21 Python