一个简单至极的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树的代码,可以嵌套任意层
Oct 09 PHP
php access 数据连接与读取保存编辑数据的实现代码
May 12 PHP
简单实现限定phpmyadmin访问ip的方法
Mar 05 PHP
解析php中mysql_connect与mysql_pconncet的区别详解
May 15 PHP
PHP时间戳 strtotime()使用方法和技巧
Oct 29 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 PHP
php中的字符编码转换函数用法示例
Oct 20 PHP
php中静态类与静态变量用法的区别分析
Jan 15 PHP
PHP购物车类Cart.class.php定义与用法示例
Jul 20 PHP
PHP实现统计所有字符在字符串中出现次数的方法
Oct 17 PHP
Yii2压缩PHP中模板代码的输出问题
Aug 28 PHP
PHP7 标准库修改
Mar 09 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下常用正则表达式整理
2010/10/26 PHP
php产生随机数的两种方法实例代码 输出随机IP
2011/04/08 PHP
ThinkPHP3.1.3版本新特性概述
2014/06/19 PHP
php之Smarty模板使用方法示例详解
2014/07/08 PHP
PHP+Mysql基于事务处理实现转账功能的方法
2015/07/08 PHP
PHP实现时间比较和时间差计算的方法示例
2017/07/24 PHP
jQuery 拖动层(在可视区域范围内)
2012/05/24 Javascript
javascript中2个感叹号的用法实例详解
2014/09/04 Javascript
项目中常用的JS方法整理
2015/01/30 Javascript
javascript实现选中复选框后相关输入框变灰不可用的方法
2015/08/11 Javascript
jQuery实现div横向拖拽排序的简单实例
2016/07/13 Javascript
JS实现六位字符密码输入器功能
2016/08/19 Javascript
JS之if语句对接事件动作逻辑(详解)
2017/06/28 Javascript
JavaScript箭头函数_动力节点Java学院整理
2017/06/28 Javascript
Angular 2父子组件数据传递之@ViewChild获取子组件详解
2017/07/04 Javascript
js for终止循环 跳出多层循环
2018/10/04 Javascript
jQuery事件多次绑定与解绑问题实例分析
2019/02/19 jQuery
JS用最简单的方法实现四舍五入
2019/08/27 Javascript
适合前端Vue开发童鞋的跨平台Weex的使用详解
2019/10/16 Javascript
使用nodeJS中的fs模块对文件及目录进行读写,删除,追加,等操作详解
2020/02/06 NodeJs
vue中利用three.js实现全景图的完整示例
2020/12/07 Vue.js
深入理解Python中的元类(metaclass)
2015/02/14 Python
Python中的super用法详解
2015/05/28 Python
Django中login_required装饰器的深入介绍
2017/11/24 Python
Python排序搜索基本算法之希尔排序实例分析
2017/12/09 Python
TensorFlow实现Softmax回归模型
2018/03/09 Python
python实现超简单的视频对象提取功能
2018/06/04 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
2019/02/21 Python
Django Celery异步任务队列的实现
2019/07/24 Python
python和pywin32实现窗口查找、遍历和点击的示例代码
2020/04/01 Python
html5 video标签屏蔽右键视频另存为的js代码
2013/11/12 HTML / CSS
工伤赔偿协议书范本
2014/04/15 职场文书
入党积极分子自我批评思想汇报
2014/10/10 职场文书
高中诗歌鉴赏教学反思
2016/02/16 职场文书
利用Selenium添加cookie实现自动登录的示例代码(fofa)
2021/05/08 Python
Python 实现定积分与二重定积分的操作
2021/05/26 Python