一个简单至极的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乱码问题,UTF-8乱码常见问题小结
Apr 09 PHP
php标签云的实现代码
Oct 10 PHP
强烈声明: 不要使用(include/require)_once
Jun 06 PHP
thinkphp 多表 事务详解
Jun 17 PHP
使用ThinkPHP+Uploadify实现图片上传功能
Jun 26 PHP
Mac环境下php操作mysql数据库的方法分享
May 11 PHP
是 WordPress 让 PHP 更流行了 而不是框架
Feb 03 PHP
浅谈php中urlencode与rawurlencode的区别
Sep 05 PHP
详解PHP中array_rand函数的使用方法
Sep 11 PHP
Laravel中任务调度console使用方法小结
May 07 PHP
php实现批量上传数据到数据库(.csv格式)的案例
Jun 18 PHP
PHP新手指南
Apr 01 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调用Oracle存储过程
2006/10/09 PHP
用PHP连接Oracle for NT 远程数据库
2006/10/09 PHP
php带密码功能并下载远程文件保存本地指定目录 修改加强版
2010/05/16 PHP
PHP永久登录、记住我功能实现方法和安全做法
2015/04/27 PHP
php常量详细解析
2015/10/27 PHP
PHP中的数组处理函数实例总结
2016/01/09 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
利用PHP获取汉字首字母并且分组排序详解
2017/10/22 PHP
javascript 精粹笔记
2010/05/09 Javascript
JavaScript初学者应注意的七个细节详细介绍
2012/12/27 Javascript
javascript作用域和闭包使用详解
2014/04/25 Javascript
JavaScript截取指定长度字符串点击可以展开全部代码
2015/12/04 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记8)
2015/12/24 Javascript
javascript创建对象的3种方法
2016/11/02 Javascript
Websocket协议详解及简单实例代码
2016/12/12 Javascript
javascript稀疏数组(sparse array)和密集数组用法分析
2016/12/28 Javascript
详解微信小程序入门五: wxml文件引用、模版、生命周期
2017/01/20 Javascript
详解angular路由高亮之RouterLinkActive
2018/04/28 Javascript
浅谈Angular6的服务和依赖注入
2018/06/27 Javascript
[02:21]DOTA2英雄基础教程 蝙蝠骑士
2013/12/16 DOTA
[48:02]Ti4循环赛第三日 VG vs Liquid和NEWBEE vs DK
2014/07/12 DOTA
python的Template使用指南
2014/09/11 Python
Python优化技巧之利用ctypes提高执行速度
2016/09/11 Python
详解Python各大聊天系统的屏蔽脏话功能原理
2016/12/01 Python
浅谈使用Python变量时要避免的3个错误
2017/10/30 Python
Python中捕获键盘的方式详解
2019/03/28 Python
如何在Cloud Studio上执行Python代码?
2019/08/09 Python
python自动化测试之异常及日志操作实例分析
2019/11/09 Python
Python函数递归调用实现原理实例解析
2020/08/11 Python
几款好用的python工具库(小结)
2020/10/20 Python
使用CSS3实现input多选框自定义样式的方法示例
2019/07/19 HTML / CSS
Glamest意大利:女性在线奢侈品零售店
2019/04/28 全球购物
信息总监管理职责范本
2014/03/08 职场文书
2014年学校党建工作汇报材料
2014/11/02 职场文书
2016学习依法治国心得体会
2016/01/15 职场文书
Python Matplotlib绘制条形图的全过程
2021/10/24 Python