一个很棒的PHP缓存类,收藏下
<?php
class Cache {
/** 缓存目录 **/
var $CacheDir = './cache';
/** 缓存的文件 **/
var $CacheFile = '';
/** 文件缓存时间(分钟) **/
var $CacheTime = 0;
/** 文件是否已缓存 **/
var $CacheFound = False;
/** 错误及调试信息 **/
var $DebugMsg = NULL; function Cache($CacheTime = 0) {
$this->CacheTime = $CacheTime;
} private function Run() {
/** 缓存时间大于0,检测缓存文件的修改时间,在缓存时间内为缓存文件名,超过缓存时间为False,
小于等于0,返回false,并清理已缓存的文件
**/
Return $this->CacheTime ? $this->CheckCacheFile() : $this->CleanCacheFile();
}
function GetCache($VistUrl,$CacheFileType = 'html')
{
$this->SetCacheFile($VistUrl,$CacheFileType); $fileName=$this->CheckCacheFile();
if($fileName)
{
$fp = fopen($fileName,"r");
$content_= fread($fp, filesize($fileName));
fclose($fp);
return $content_;
}
else
{
return false;
}
}
private function SetCacheFile($VistUrl,$CacheFileType = 'html') {
if(empty($VistUrl)) {
/** 默认为index.html **/
$this->CacheFile = 'index';
}else {
/** 传递参数为$_POST时 **/
$this->CacheFile = is_array($VistUrl) ? implode('.',$VistUrl) : $VistUrl;
}
$this->CacheFile = $this->CacheDir.'/'.md5($this->CacheFile);
$this->CacheFile.= '.'.$CacheFileType;
} function SetCacheTime($t = 60) {
$this->CacheTime = $t;
} private function CheckCacheFile() {
if(!$this->CacheTime || !file_exists($this->CacheFile)) {Return False;}
/** 比较文件的建立/修改日期和当前日期的时间差 **/
$GetTime=(Time()-Filemtime($this->CacheFile))/(60*1);
/** Filemtime函数有缓存,注意清理 **/
Clearstatcache();
$this->Debug('Time Limit '.($GetTime*60).'/'.($this->CacheTime*60).'');
$this->CacheFound = $GetTime <= $this->CacheTime ? $this->CacheFile : False;
Return $this->CacheFound;
} function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') {
$this->SetCacheFile($VistUrl,$CacheFileType);
if(!$this->CacheTime) {
Return False;
}
/** 检测缓存目录是否存在 **/
if(true === $this->CheckCacheDir()) {
$CacheFile = $this->CacheFile;
$CacheFile = str_replace('//','/',$CacheFile);
$fp = @fopen($CacheFile,"wb");
if(!$fp) {
$this->Debug('Open File '.$CacheFile.' Fail');
}else {
if(@!fwrite($fp,$Content)){
$this->Debug('Write '.$CacheFile.' Fail');
}else {
$this->Debug('Cached File');
};
@fclose($fp);
}
}else {
/** 缓存目录不存在,或不能建立目录 **/
$this->Debug('Cache Folder '.$this->CacheDir.' Not Found');
}
} private function CheckCacheDir() {
if(file_exists($this->CacheDir)) { Return true; }
/** 保存当前工作目录 **/
$Location = getcwd();
/** 把路径划分成单个目录 **/
$Dir = split("/", $this->CacheDir);
/** 循环建立目录 **/
$CatchErr = True;
for ($i=0; $i<count($Dir); $i++){
if (!file_exists($Dir[$i])){
/** 建立目录失败会返回False 返回建立最后一个目录的返回值 **/
$CatchErr = @mkdir($Dir[$i],0777);
}
@chdir($Dir[$i]);
}
/** 建立完成后要切换到原目录 **/
chdir($Location);
if(!$CatchErr) {
$this->Debug('Create Folder '.$this->CacheDir.' Fail');
}
Return $CatchErr;
} private function CleanCacheFile() {
if(file_exists($this->CacheFile)) {
@chmod($this->CacheFile,777);
@unlink($this->CacheFile);
}
/** 置没有缓存文件 **/
$this->CacheFound = False;
Return $this->CacheFound;
} function Debug($msg='') {
if(DEBUG) {
$this->DebugMsg[] = '[Cache]'.$msg;
}
} function GetError() {
Return empty($this->DebugMsg) ? '' : "<br>n".implode("<br>n",$this->DebugMsg);
}
}/* end of class */
?>
一个很棒的PHP缓存类,收藏下的更多相关文章
- 国外大神制作的一个很棒的matplotlib 可视化教程
国外大神制作的一个很棒的matplotlib 可视化教程 参考:https://www.machinelearningplus.com/plots/top-50-matplotlib-visualiz ...
- Makefile经典教程(一个很棒很清晰的讲解)【转】
转自:https://blog.csdn.net/seven_amber/article/details/70216216 该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神 ...
- MD4C/CO46/MD04一个很棒的工单缺料分析
大家好~~~ 之前在MD04物料分析的时候,看到有订单预留,双击有个订单报告可以显示一个订单物料是否缺料清单 这个单独的工单分析可以在T-code:MD4C,CO46查看,如果只是单独的使用,那么这两 ...
- Linux 下一个很棒的命令行工具
导读 Taskwarrior 是 Ubuntu/Linux 下一个简单而直接的基于命令行的 TODO 工具.这个开源软件是我曾用过的最简单的基于命令行的工具之一.Taskwarrior 可以帮助你更好 ...
- 一个简单至极的PHP缓存类代码
https://www.jb51.net/article/73836.htm 直接看代码吧!使用说明:1.实例化$cache = new Cache(); 2.设置缓存时间和缓存目录$cache = ...
- 如何构建一个很棒网站页脚(Website Footer)
对于很多人来说,制作页脚是只是设计师顺手而为的任务.它似乎很容易,似乎可以忽略不计.因为很多人都觉得网站底部很少人会去看,而且页脚链接的所有链接的点击率(CTR)都是最低的,何必呢? 真是这样的吗?下 ...
- 推荐一个很棒的开源工作流elsa-core
开源项目orchard主要开发人员Sipke Schoorstra 开源了一个netcore 工作流项目,地址:https://github.com/elsa-workflows/elsa-core, ...
- 推荐一个很棒的JS绘图库Flot
Flot是Ole Laursen开发的基于JQuery的纯JavaScript实现的绘图库,Flot使用起来非常简单,绘图效果相当绚丽,而且还支持一些图片的操作功能,例如图片的缩放.可以看一下Flot ...
- 最近在学习UDP方面的通信,找到一个很棒的博客
http://blog.csdn.net/kesalin/article/details/8798039
随机推荐
- Python基础学习三 字典、元组
一.元组 元组,提示别人,这个值是不能被改变的,元组的定义方式是用(),小括号: 元组只有两个方法,那就是count和index mysql1 = ('127.0.0.1',3306,'my','ro ...
- Django的views使用
这里介绍一下Django中常用的类视图,主要说明在视图中如何接收和传递参数.返回到页面等. 注意,使用这些类视图时,在url中需要加上.as_view(). 我将介绍的内容分为三部分:django的V ...
- 第一篇 UCOS介绍
第一篇 UCOS介绍 这个大家都知道.呵呵.考虑到咱们学习的完整性还是在这里唠叨一下.让大家再熟悉一下.高手们忍耐一下吧! uC/OS II(Micro Control Operation Syste ...
- android 4.0.4系统下实现apk的静默安装和启动
转 android 4.0.4系统下实现apk的静默安装和启动 分类: Android 2013-02-14 14:13 1762人阅读 评论(10) 收藏 举报 最近在android 4.0.4系统 ...
- JAVA基础知识总结15(集合容器)
集合框架:用于存储数据的容器. 1:对象封装数据,对象多了也需要存储.集合用于存储对象. 2:对象的个数确定可以使用数组,但是不确定怎么办?可以用集合.因为集合是可变长度的. 集合和数组的区别: 1: ...
- 【bzoj2115】[Wc2011] Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2512 Solved: 1049[Submit][Status ...
- 在页面完成读取EXCEL
protected void btnUpload_Click(object sender, EventArgs e) { if (Page.IsValid) { string sFILENAME = ...
- mongo_1 新手之路
mongodb 进入.在bin目录下 至于安装各位自己查资料吧 .不废话了 直接上图.这种表示已经成功进入mongo 本人mongo 数据库存放地址.如有需要可以清空可以自己删除. 接下来就是 ...
- Python2.7的安装、python3的安装
>登录python官网下载python2.7的相关版本 python官网链接 >根据平台选择相应的版本 >下载完毕后点击安装即可 >配置环境变量 >安装成功 2 pyth ...
- Java 基于web service 暴露接口 供外部调用
package cn.zr.out.outinterface; import java.text.SimpleDateFormat; import java.util.Date; import jav ...