一个简单至极的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
在线增减.htpasswd内的用户
Oct 09 PHP
通俗易懂的php防注入代码
Apr 07 PHP
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
Nov 18 PHP
php使用pdo连接并查询sql数据库的方法
Dec 24 PHP
PHP设计模式之装饰者模式代码实例
May 11 PHP
Zend Framework动作助手Redirector用法实例详解
Mar 05 PHP
Zend Framework基本页面布局分析
Mar 19 PHP
thinkPHP实现将excel导入到数据库中的方法
Apr 22 PHP
Yii2中OAuth扩展及QQ互联登录实现方法
May 16 PHP
PHP设计模式之装饰器(装饰者)模式(Decorator)入门与应用详解
Dec 13 PHP
php 解析非标准json、非规范json
Apr 01 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
判“新”函数:得到今天与明天的秒数
2006/10/09 PHP
PHP 判断常量,变量和函数是否存在
2009/04/26 PHP
php数据库连接时容易出错的特殊符号问题
2010/09/01 PHP
php随机输出名人名言的代码
2012/10/07 PHP
php微信开发之谷歌测距
2018/06/14 PHP
PHP中define() 与 const定义常量的区别详解
2019/06/25 PHP
一个简单的js树形菜单
2011/12/09 Javascript
jQuery添加/改变/移除CSS类及判断是否已经存在CSS
2014/08/20 Javascript
jquery实现点击弹出带标题栏的弹出层(从右上角飞入)效果
2015/09/19 Javascript
JavaScript测试工具之Karma-Jasmine的安装和使用详解
2015/12/03 Javascript
微信小程序 图片加载(本地,网路)实例详解
2017/03/10 Javascript
vuejs使用FormData实现ajax上传图片文件
2017/08/08 Javascript
jquery操作ul的一些操作笔记整理(干货)
2017/08/31 jQuery
纯JS实现可用于页码更换的飞页特效示例
2018/05/21 Javascript
Vue数据绑定简析小结
2019/05/07 Javascript
简单了解JavaScript中常见的反模式
2019/06/21 Javascript
微信小程序实现录音功能
2019/11/22 Javascript
微信小程序request请求封装,验签代码实例
2019/12/04 Javascript
使用python调用浏览器并打开一个网址的例子
2014/06/05 Python
Django中实现一个高性能计数器(Counter)实例
2014/07/09 Python
RC4文件加密的python实现方法
2015/06/30 Python
Python实现备份MySQL数据库的方法示例
2018/01/11 Python
Python实现钉钉发送报警消息的方法
2019/02/20 Python
Python 获取windows桌面路径的5种方法小结
2019/07/15 Python
英格兰足协官方商店:England Store
2019/07/12 全球购物
英国排名第一的LED灯泡网站:LED Bulbs
2019/09/03 全球购物
什么是Oracle的后台进程background processes?都有哪些后台进程?
2012/04/26 面试题
shell程序如何生命变量?shell变量是弱变量吗?
2014/11/10 面试题
努力学习演讲稿
2014/05/10 职场文书
2014组织生活会方案
2014/05/19 职场文书
医学专业毕业生求职信
2014/06/20 职场文书
六查六看六改心得体会
2014/10/14 职场文书
2014年纪委工作总结
2014/12/05 职场文书
2014年高一班主任工作总结
2014/12/05 职场文书
领导莅临指导欢迎词
2015/09/30 职场文书
postgresql如何找到表中重复数据的行并删除
2023/05/08 MySQL