<?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. Tornado 高并发源码分析之五--- IOLoop 对象

    IOLoop主要工作 1.将TCPServer 注册到 IOLoop 的事件记到 _handlers 字段,同时注册 READ 和 ERROR 事件到 epoll 2.IOLoop 启动一个大循环,负 ...

  2. XML文件中“>”和“<”的转码

    在xml文件中,大于号“>”和小于号“<”是不能被直接识别的,需要经过转码才可以被识别,转码后的格式如下: < < 小于 > > 大于

  3. 1-5 构建官方example-Windows平台

    https://github.com/facebook/react-native https://github.com/facebook/react-native.git  https://githu ...

  4. 图论算法》关于SPFA和Dijkstra算法的两三事

    本来我是想把这两个算法分开写描述的,但是SPFA其实就是Dijkstra的稀疏图优化,所以其实代码差不多,所以就放在一起写了. 因为SPFA是Dijkstra的优化,所以我想来讲讲Dijkstra. ...

  5. 用Eclipse Memory Analyzer查找内存泄露

    写在CSDN里面了 http://blog.csdn.net/dayulxl/article/details/78164301

  6. java简单的测试方法执行了多少时间

    (1)以毫秒为单位的 long startTime = System.currentTimeMillis(); // 获取开始时间 // doThing(); // 测试的代码段 long endTi ...

  7. POI 生成exel报表

    去官网下载相关jar包   http://poi.apache.org/ package poi.zr.com; import java.io.File; import java.io.FileNot ...

  8. hook NtQueryDirectoryFile实现文件隐藏

    一.NtQueryDirectoryFile函数功能(NT系列函数) NtQueryDirectoryFile函数:在一个给定的文件句柄,该函数返回该文件句柄指定目录下的不同文件的各种信息. 根据传入 ...

  9. aspx页面上输出xml的问题

    在aspx页面上输出xml,需要在后台输出编码格式和编码类型,如下代码: Response.Charset = "utf-8";//格式Response.ContentType = ...

  10. 【未整理】web.xml加载顺序.RP

    一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...