一个简单至极的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删除数组中的特定元素的代码
Jun 28 PHP
php中判断文件空目录是否有读写权限的函数代码
Aug 07 PHP
php在程序中将网页生成word文档并提供下载的代码
Oct 09 PHP
控制PHP的输出:缓存并压缩动态页面
Jun 11 PHP
php下拉选项的批量操作的实现代码
Oct 14 PHP
Laravel 5框架学习之子视图和表单复用
Apr 09 PHP
php实现json编码的方法
Jul 30 PHP
Yii核心验证器api详解
Nov 23 PHP
PHP实现的登录页面信息提示功能示例
Jul 24 PHP
原生JS实现Ajax通过GET方式与PHP进行交互操作示例
May 12 PHP
在Laravel5中正确设置文件权限的方法
May 22 PHP
php实现的证件照换底色功能示例【人像抠图/换背景图】
May 29 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中用header图片地址 简单隐藏图片源地址
2008/04/09 PHP
php通过文件头检测文件类型通用代码类(zip,rar等)
2010/10/19 PHP
支持生僻字且自动识别utf-8编码的php汉字转拼音类
2014/06/27 PHP
THINKPHP项目开发中的日志记录实例分析
2014/12/01 PHP
用javascript实现读取txt文档的脚本
2007/07/20 Javascript
jqTransform form表单美化插件使用方法
2012/07/05 Javascript
javascript轻量级模板引擎juicer使用指南
2014/06/22 Javascript
EasyUI实现第二层弹出框的方法
2015/03/01 Javascript
Jsonp post 跨域方案
2015/07/06 Javascript
原生JS实现N级菜单的代码
2017/05/21 Javascript
Vue.js进行查询操作的实例详解
2017/08/25 Javascript
详解js删除数组中的指定元素
2018/10/31 Javascript
JavaScript中import用法总结
2019/01/20 Javascript
使用mixins实现elementUI表单全局验证的解决方法
2019/04/02 Javascript
前端天气插件tpwidget使用方法详解
2019/06/24 Javascript
nodejs语言实现验证码生成功能的示例代码
2019/10/13 NodeJs
详解vue中$nextTick和$forceUpdate的用法
2019/12/11 Javascript
react组件基本用法示例小结
2020/04/27 Javascript
js+canvas实现图片格式webp/png/jpeg在线转换
2020/08/22 Javascript
极简的Python入门指引
2015/04/01 Python
详解Python if-elif-else知识点
2018/06/11 Python
使用Python 正则匹配两个特定字符之间的字符方法
2018/12/24 Python
python对Excel的读取的示例代码
2020/02/14 Python
keras 回调函数Callbacks 断点ModelCheckpoint教程
2020/06/18 Python
Canvas高级路径操作之拖拽对象的实现
2019/08/05 HTML / CSS
美国医疗用品、医疗设备和家庭保健用品商店:Medical Supply Depot
2018/07/08 全球购物
英国乐购杂货:Tesco Groceries
2018/11/29 全球购物
德国最新街头服饰网上商店:BODYCHECK
2019/09/15 全球购物
师范生自荐信范文
2013/10/06 职场文书
大家检讨书5000字
2014/02/03 职场文书
小学生植树节活动总结
2014/07/04 职场文书
见习报告的格式
2014/11/04 职场文书
优秀学生干部事迹材料
2014/12/24 职场文书
申报材料格式
2014/12/30 职场文书
少年派的奇幻漂流观后感
2015/06/08 职场文书
2019年度开业庆典祝福语大全!
2019/07/05 职场文书