一个简单至极的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 相关文章推荐
比较discuz和ecshop的截取字符串函数php版
Sep 03 PHP
php cc攻击代码与防范方法
Oct 18 PHP
使用PHP计算两个路径的相对路径
Jun 14 PHP
php实现图形显示Ip地址的代码及注释
Jan 20 PHP
PHP生成条形图的方法
Dec 10 PHP
PHP+AJAX实现投票功能的方法
Sep 28 PHP
PHP实现微信网页授权开发教程
Jan 19 PHP
ThinkPHP5实现作业管理系统中处理学生未交作业与已交作业信息的方法
Nov 12 PHP
Yii框架用户登录session丢失问题解决方法
Jan 07 PHP
PHP连接MYSQL数据库的3种常用方法
Feb 27 PHP
PHP Laravel 上传图片、文件等类封装
Aug 16 PHP
利用 fsockopen() 函数开放端口扫描器的实例
Aug 19 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统计nginx访问日志中的搜索引擎抓取404链接页面路径
2014/06/30 PHP
百度工程师讲PHP函数的实现原理及性能分析(一)
2015/05/13 PHP
PHP简单实现二维数组的矩阵转置操作示例
2017/11/24 PHP
PHP7 list() 函数修改
2021/03/09 PHP
js onpropertychange输入框 事件获取属性
2009/03/26 Javascript
javascript(jquery)利用函数修改全局变量的代码
2009/11/02 Javascript
利用JS自动打开页面上链接的实现代码
2011/09/25 Javascript
javaScript复制功能调用实现方案
2012/12/13 Javascript
jquery弹窗插件colorbox绑定动态生成元素的方法
2014/06/20 Javascript
jQuery操作表单常用控件方法小结
2015/03/23 Javascript
jQuery实现带动画效果的多级下拉菜单代码
2015/09/08 Javascript
js实现文本框输入文字个数限制代码
2015/12/25 Javascript
AngularJS基础 ng-src 指令简单示例
2016/08/03 Javascript
JS表格组件神器bootstrap table使用指南详解
2017/04/12 Javascript
AngularJS与BootStrap模仿百度分页的示例代码
2018/05/23 Javascript
react-native动态切换tab组件的方法
2018/07/07 Javascript
实例详解ztree在vue项目中使用并且带有搜索功能
2018/08/24 Javascript
Vue使用axios出现options请求方法
2019/05/30 Javascript
jQuery实现简单聊天室
2020/02/08 jQuery
js实现浏览器打印功能的示例代码
2020/07/15 Javascript
在antd Form表单中select设置初始值操作
2020/11/02 Javascript
PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
2019/06/17 Python
django admin管理工具自定义时间区间筛选器DateRangeFilter介绍
2020/05/19 Python
Python实现敏感词过滤的4种方法
2020/09/12 Python
HTML5跳转小程序wx-open-launch-weapp的示例代码
2020/07/16 HTML / CSS
Java面向对象面试题
2016/12/26 面试题
员工工作表扬信范文
2014/01/13 职场文书
大学生实习证明范本
2014/01/15 职场文书
户外拓展活动方案
2014/02/11 职场文书
厂长岗位职责
2014/02/19 职场文书
学院党的群众路线教育实践活动第一阶段情况汇报
2014/10/25 职场文书
2015暑假打工实践报告
2015/07/13 职场文书
2016大学生入党积极分子心得体会
2016/01/06 职场文书
八年级语文教学反思
2016/03/03 职场文书
2019各种承诺书范文
2019/06/24 职场文书
浅谈CSS不规则边框的生成方案
2021/05/25 HTML / CSS