一个简单至极的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生成静态页面详解
Dec 05 PHP
DedeCMS dede_channeltype表字段注释
Apr 07 PHP
利用PHP实现智能文件类型检测的实现代码
Aug 02 PHP
php中$_REQUEST、$_POST、$_GET的区别和联系小结
Nov 23 PHP
支持中文的php加密解密类代码
Nov 27 PHP
IIS安装Apache伪静态插件的具体操作图文
Jul 01 PHP
推荐几款用 Sublime Text 开发 Laravel 所用到的插件
Oct 30 PHP
解决laravel 5.1报错:No supported encrypter found的办法
Jun 07 PHP
PHP之多条件混合筛选功能的实现方法
Oct 09 PHP
laravel csrf排除路由,禁止,关闭指定路由的例子
Oct 21 PHP
tp5.1 框架数据库高级查询技巧实例总结
May 25 PHP
php解析非标准json、非规范json的方式实例
May 10 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
基于Windows下Apache PHP5.3.1安装教程
2010/01/08 PHP
PHP静态文件生成类实例
2014/11/29 PHP
Web程序工作原理详解
2014/12/25 PHP
php发送邮件的问题详解
2015/06/22 PHP
WordPress中转义HTML与过滤链接的相关PHP函数使用解析
2015/12/22 PHP
php实现购物车功能(下)
2016/01/05 PHP
PHP中数组转换为SimpleXML教程
2019/01/27 PHP
Laravel框架实现多数据库连接操作详解
2019/07/12 PHP
弹出广告特效代码(一个IP只弹出一次)
2007/05/11 Javascript
jquery下json数组的操作实现代码
2010/08/09 Javascript
jquery中实现标签切换效果的代码
2011/03/01 Javascript
jquery 操作两个select实现值之间的互相传递
2014/03/07 Javascript
asp.net中oracle 存储过程(图文)
2015/08/12 Javascript
JavaScript基本类型值-Number类型
2017/02/24 Javascript
利用VS Code开发你的第一个AngularJS 2应用程序
2017/12/15 Javascript
vue 标签属性数据绑定和拼接的实现方法
2018/05/17 Javascript
JS加密插件CryptoJS实现的DES加密示例
2018/08/16 Javascript
详解vue使用插槽分发内容slot的用法
2019/03/28 Javascript
Vue动态创建注册component的实例代码
2019/06/14 Javascript
es6中reduce的基本使用方法
2019/09/10 Javascript
Python写的一个简单DNS服务器实例
2014/06/04 Python
跟老齐学Python之深入变量和引用对象
2014/09/24 Python
Django发送html邮件的方法
2015/05/26 Python
python3学习笔记之多进程分布式小例子
2018/02/13 Python
Python3安装Pillow与PIL的方法
2019/04/03 Python
Python实现平行坐标图的两种方法小结
2019/07/04 Python
django 连接数据库出现1045错误的解决方式
2020/05/14 Python
Bugatchi官方网站:男士服装在线
2019/04/10 全球购物
创业计划书中要认真思考的问题
2013/12/28 职场文书
《童年》教学反思
2014/02/18 职场文书
教师党员一句话承诺
2014/03/28 职场文书
先进事迹报告会主持词
2014/04/02 职场文书
行政监察建议书
2014/05/19 职场文书
实施意见格式范本
2015/06/05 职场文书
2016年中秋祝酒词
2015/11/26 职场文书
教你怎么用Python生成九宫格照片
2021/05/20 Python