一个简单至极的PHP缓存类代码
https://www.jb51.net/article/73836.htm
直接看代码吧!
使用说明:
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缓存类代码的更多相关文章
- Qt5.9一个简单的多线程实例(类QThread)(第一种方法)
		
Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...
 - 实现一个简单的http请求工具类
		
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
 - 使用QT实现一个简单的登陆对话框(纯代码实现C++)
		
使用QT实现一个简单的登陆对话框(纯代码实现C++) 效果展示 使用的QT控件 控件 描述 QLable 标签 QLineEdit 行文本框 QPushButton 按扭 QHBoxLayout 水平 ...
 - 简单的php数据库操作类代码(增,删,改,查)
		
这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...
 - 一个简单小巧的CSV读取类
		
最近在基于亚马逊MWS API做一些服务,需要读取亚马逊返回的报表,是一个按照\t分割的文本,所以就封装了一个简单小巧的CsvReader类 使用方法 使用方法非常简单,只需要传递一个stream子类 ...
 - 一个简单IP防刷工具类, x秒内最多允许y次单ip操作
		
IP防刷,也就是在短时间内有大量相同ip的请求,可能是恶意的,也可能是超出业务范围的.总之,我们需要杜绝短时间内大量请求的问题,怎么处理? 其实这个问题,真的是太常见和太简单了,但是真正来做的时候,可 ...
 - Java下一个简单的数据库分库帮助类
		
简介 前面两篇文章主要讲了数据库读写分离和分表分库的一些问题,这篇文章主要讲一下我个人实现的一个分表分库项目. 在此之前,我有写过一个.Net的分库,最近在做Java的项目,就顺便做出一 ...
 - 【如何快速的开发一个简单的iOS直播app】(代码篇)
		
开篇([如何快速的开发一个完整的iOS直播app](原理篇)) 好久没写简书,因为好奇的我跑去学习直播了,今天就分享一下我的感慨. 目前为止直播还是比较热点的技术的,简书,git上有几篇阅读量和含金量 ...
 - 一个简单的异常/条件重试类(C#)
		
单个类,简单好用 using System; using System.Linq.Expressions; using System.Threading; using System.Threading ...
 
随机推荐
- 洛谷P1463 反素数
			
经典题了,很难想到这TM是搜索...... 题意:求[1, n]中约数最多的数中最小的. 解:我们有约数个数定理. 所以考虑通过枚举每个质因数个数来直接计算出约数个数. 然后就可以搜索了. 注意:若p ...
 - 由AC自动机引发的灵感
			
一,WA自动机 #include <cstdio> using namespace std; int main() { printf("☺"); ; } 二,TLE自动 ...
 - .Net Core Nlog日志记录到MySql
			
前段时间想要实现这个功能网上找了很多资料,现在整理一下发布出来,希望给大家一点帮助. 首先是依赖项的选择: 关于NLog版本不是最新是因为最新版本有点问题我试了试不支持,所以选了这几个版本,MySql ...
 - 题解-洛谷P1020P导弹拦截(求单调序列长度的优化)
			
https://www.luogu.org/problemnew/show/P1020 (原题链接) 第一问就是求最长不上升子序列的长度,自然就想到了c++一本通里动态规划里O(n^2)的算法,但题目 ...
 - tensorflow不同版本安装与升级/降级
			
https://blog.csdn.net/junmuzi/article/details/78357371 首先,可以安装一个anaconda. 然后使用python的pip可以安装特定版本的ten ...
 - appium desktop 1.7 的swipe功能不能用,重写。
			
rt // @Override public void swipe(int startx,int starty,int endx,int endy,int ms){ Duration duration ...
 - spring的设计模式
			
spring中用到哪些设计模式 1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFacto ...
 - 【强大的Java集成开发工具】MyEclipse 2015 Stable 2.0 for Mac
			
[简介] MyEclipse是一款 Mac 上的Java 强大的集成开发工具,今天和大家分享最新的 MyEclipse 2015 Stable 2.0 版本,MyEclipse 2015 基于 Ecl ...
 - Mac 软件专题:教学参考工具软件-外语/医学/天文/地理/数学等
			
今天和大家分享mac软件专题:教学参考工具软件,在这个专题中,主要向大家推荐一些Mac上优秀的教育教学.知识参考类的软件,包含外语.医学.天文.地址.数学.音乐等方面,学生.老师以及相关的工作者不要错 ...
 - Hadoop生态圈-单点登录框架之CAS(Central Authentication Service)部署
			
Hadoop生态圈-单点登录框架之CAS(Central Authentication Service)部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.CAS简介 CAS( ...