一个简单至极的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 01 PHP
php中的三元运算符使用说明
Jul 03 PHP
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
Mar 01 PHP
Window下PHP三种运行方式图文详解
Jun 11 PHP
PHP中常用的字符串格式化函数总结
Nov 19 PHP
PHP生成随机字符串(3种方法)
Sep 25 PHP
在Mac OS的PHP环境下安装配置MemCache的全过程解析
Feb 15 PHP
PHP 配置后台登录以及模板引入
Jan 24 PHP
PHP 中使用explode()函数切割字符串为数组的示例
May 06 PHP
PHP操作MongoDB实现增删改查功能【附php7操作MongoDB方法】
Apr 24 PHP
php面向对象基础详解【星际争霸游戏案例】
Jan 23 PHP
Yii使用EasyWechat实现小程序获取用户的openID的方法
Apr 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
简单示例AJAX结合PHP代码实现登录效果代码
2008/07/25 PHP
Win2003+apache+PHP+SqlServer2008 配置生产环境
2014/07/29 PHP
Linux下安装PHP MSSQL扩展教程
2014/10/24 PHP
PHP中把对象数组转换成普通数组的方法
2015/07/10 PHP
解决Laravel blade模板转义html标签的问题
2019/09/03 PHP
Ext第一周 史上最强学习笔记---GridPanel(基础篇)
2008/12/29 Javascript
原生js实现查找/添加/删除/指定元素的class
2013/04/12 Javascript
AngularJS模块管理问题的非常规处理方法
2015/04/29 Javascript
纯js实现手风琴效果
2020/04/17 Javascript
jQuery 常用代码集锦(必看篇)
2016/05/16 Javascript
深入理解JavaScript中的浮点数
2016/05/18 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
轻松学习Javascript闭包
2017/03/01 Javascript
JS去掉字符串末尾的标点符号及删除最后一个字符的方法
2017/10/24 Javascript
详解vue-cli项目中的proxyTable跨域问题小结
2018/02/09 Javascript
详解VUE里子组件如何获取父组件动态变化的值
2018/12/26 Javascript
ES6 Array常用扩展的应用实例分析
2019/06/26 Javascript
微信内置开发 iOS修改键盘换行为搜索的解决方案
2019/11/06 Javascript
使用React代码动态生成栅格布局的方法
2020/05/24 Javascript
vue中destroyed方法的使用说明
2020/07/21 Javascript
JavaScript this关键字指向常用情况解析
2020/09/02 Javascript
javascript实现拼图游戏
2021/01/29 Javascript
python中requests模块的使用方法
2015/04/08 Python
简单了解python中对象的取反运算符
2019/07/01 Python
Python基础之变量基本用法与进阶详解
2020/01/03 Python
.dcm格式文件软件读取及python处理详解
2020/01/16 Python
使用Python内置模块与函数进行不同进制的数的转换
2020/04/26 Python
Python PyQt5模块实现窗口GUI界面代码实例
2020/05/12 Python
深入浅析Python代码规范性检测
2020/07/31 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
2020/12/07 Python
HTML5 canvas基本绘图之绘制五角星
2016/06/27 HTML / CSS
Perfume’s Club中文官网:西班牙美妆在线零售品牌
2020/08/24 全球购物
优秀应届本科生求职信
2014/07/19 职场文书
暑期培训班策划方案
2014/08/26 职场文书
高中生综合素质自我评价
2015/03/06 职场文书
Vue中Object.assign清空数据报错的解决方案
2022/03/03 Vue.js