一个简单至极的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 和 MySQL 基础教程(四)
Oct 09 PHP
PHP开发框架总结收藏
Apr 24 PHP
用Php编写注册后Email激活验证的实例代码
Mar 11 PHP
php curl的深入解析
Jun 02 PHP
使用cookie实现统计访问者登陆次数
Jun 08 PHP
codeigniter中测试通过的分页类示例
Apr 17 PHP
PHP之uniqid()函数用法
Nov 03 PHP
PHP调用wsdl文件类型的接口代码分享
Nov 19 PHP
Yii2隐藏frontend/web和backend/web的方法
Dec 12 PHP
php 的反射详解及示例代码
Aug 25 PHP
php简单统计中文个数的方法
Sep 30 PHP
一文搞懂php的垃圾回收机制
Jun 18 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
打造计数器DIY三步曲(中)
2006/10/09 PHP
PHP解析目录路径的3个函数总结
2014/11/18 PHP
初识laravel5
2015/03/02 PHP
php如何比较两个浮点数是否相等详解
2019/02/12 PHP
Dom加载让图片加载完再执行的脚本代码
2008/05/15 Javascript
一个选择最快的服务器转向代码
2009/04/27 Javascript
JavaScript 空位补零实现代码
2010/02/26 Javascript
JavaScript使用过程中需要注意的地方和一些基本语法
2010/08/26 Javascript
jquery插件制作 手风琴Panel效果实现
2012/08/17 Javascript
尝试在让script的type属性等于text/html
2013/01/15 Javascript
JavaScript学习笔记之JS对象
2015/01/22 Javascript
window.open()实现post传递参数
2015/03/12 Javascript
从0开始学Vue
2016/10/27 Javascript
ajax 提交数据到后台jsp页面及页面跳转问题
2017/01/19 Javascript
jQuery实现的表格前端排序功能示例
2017/09/18 jQuery
JS+CSS实现网页加载中的动画效果
2017/10/27 Javascript
对angularJs中2种自定义服务的实例讲解
2018/09/30 Javascript
微信小程序picker组件关于objectArray数据类型的绑定方法
2019/03/13 Javascript
简单通过settimeout看javascript的运行机制
2019/05/10 Javascript
用Python实现QQ游戏大家来找茬辅助工具
2014/09/14 Python
Python语法快速入门指南
2015/10/12 Python
Python模块搜索路径代码详解
2018/01/29 Python
Python查看微信撤回消息代码
2018/06/07 Python
浅析Django中关于session的使用
2019/12/30 Python
python Pexpect模块的使用
2020/12/25 Python
Proenza Schouler官方网站:纽约女装和配饰品牌
2019/01/03 全球购物
Rentalcars.com中国:世界上最大的在线汽车租赁服务
2019/08/22 全球购物
研究生自荐信
2013/10/09 职场文书
司机检讨书
2014/02/13 职场文书
促销活动总结范文
2014/04/30 职场文书
体育专业大学生职业生涯规划范文:打造自己的运动帝国
2014/09/12 职场文书
镇党委书记群众路线整改措施思想汇报
2014/10/13 职场文书
解除租房协议书
2014/12/03 职场文书
离开雷锋的日子观后感
2015/06/09 职场文书
实例详解Python的进程,线程和协程
2022/03/13 Python
OpenStack虚拟机快照和增量备份实现方法
2022/04/04 Servers