<?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缓存类,收藏下的更多相关文章

  1. 国外大神制作的一个很棒的matplotlib 可视化教程

    国外大神制作的一个很棒的matplotlib 可视化教程 参考:https://www.machinelearningplus.com/plots/top-50-matplotlib-visualiz ...

  2. Makefile经典教程(一个很棒很清晰的讲解)【转】

    转自:https://blog.csdn.net/seven_amber/article/details/70216216 该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神 ...

  3. MD4C/CO46/MD04一个很棒的工单缺料分析

    大家好~~~ 之前在MD04物料分析的时候,看到有订单预留,双击有个订单报告可以显示一个订单物料是否缺料清单 这个单独的工单分析可以在T-code:MD4C,CO46查看,如果只是单独的使用,那么这两 ...

  4. Linux 下一个很棒的命令行工具

    导读 Taskwarrior 是 Ubuntu/Linux 下一个简单而直接的基于命令行的 TODO 工具.这个开源软件是我曾用过的最简单的基于命令行的工具之一.Taskwarrior 可以帮助你更好 ...

  5. 一个简单至极的PHP缓存类代码

    https://www.jb51.net/article/73836.htm 直接看代码吧!使用说明:1.实例化$cache = new Cache(); 2.设置缓存时间和缓存目录$cache = ...

  6. 如何构建一个很棒网站页脚(Website Footer)

    对于很多人来说,制作页脚是只是设计师顺手而为的任务.它似乎很容易,似乎可以忽略不计.因为很多人都觉得网站底部很少人会去看,而且页脚链接的所有链接的点击率(CTR)都是最低的,何必呢? 真是这样的吗?下 ...

  7. 推荐一个很棒的开源工作流elsa-core

    开源项目orchard主要开发人员Sipke Schoorstra 开源了一个netcore 工作流项目,地址:https://github.com/elsa-workflows/elsa-core, ...

  8. 推荐一个很棒的JS绘图库Flot

    Flot是Ole Laursen开发的基于JQuery的纯JavaScript实现的绘图库,Flot使用起来非常简单,绘图效果相当绚丽,而且还支持一些图片的操作功能,例如图片的缩放.可以看一下Flot ...

  9. 最近在学习UDP方面的通信,找到一个很棒的博客

    http://blog.csdn.net/kesalin/article/details/8798039

随机推荐

  1. 你不知道的js异步、作用域、闭包

    例题如下: for (var i = 0; i < 3; i++) {     setTimeout(function() {         console.log(i);     }, 0) ...

  2. Spring Cloud Eureka 3 (Eureka client注册服务提供者)

    在完成服务注册中心的搭建后我们来尝试下将一个既有的spring boot应用加入eureka的服务治理体系中 新建一个spring boot项目加入eureka client依赖 这里加入的eurek ...

  3. CrackMe的简单破解

    [CrackMe的简单破解] 对于以下这样的输入账号和密码的窗口,我们可以猜测该程序使用最简单的机制实现,即用strmp来比较用户输入的密码和原始密码匹配.所以为了破解该程序,可以通过bp strmp ...

  4. 【总结整理】pv、uv

    1.pv的全称是page view,译为页面浏览量或点击量,通常是衡量一个网站甚至一条网络新闻的指标.用户每次对网站中的一个页面的请求或访问均被记录1个PV,用户对同一页面的多次访问,pv累计.例如, ...

  5. 面试题:TCP协议三次握手

    一.首先了解TCP报文格式 其中必须了解的字段有: 1.源端口与目的端口:16位,标识出发送端与接收端的端口号. 2.序号:32位,也叫顺序号.seg序号,本报文段所发送的数据的第一个字节的序号,用来 ...

  6. Sublime Text notes

    1. 设置在窗口右下方显示文件的编码,在user preferences里加上以下的配置 2.设置用新标签页打开新文件而不是用新窗口打开,将以下配置改为false(默认为true)

  7. 局部变量和static变量的区别

    static int a ; int b; scanf_s("%d %d",&a,&b); 01374212 lea eax,[b] 01374215 push e ...

  8. CF 1091E New Year and the Factorisation Collaboration

    昨晚Good Bye 2018D题没做出来,车翻大了…… 官方题解      传送门 初赛知识:一个无向图所有顶点度数之和为偶数.然而这东西还有一个高端的名字:Handshaking lemma 但是 ...

  9. Luogu 3206 [HNOI2010]城市建设

    BZOJ 2001 很神仙的cdq分治 先放论文的链接   顾昱洲_浅谈一类分治算法 我们考虑分治询问,用$solve(l, r)$表示询问编号在$[l, r]$时的情况,那么当$l == r$的时候 ...

  10. Django框架 之 logging配置

    Django框架 之 logging配置 logging配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...