一个很棒的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
随机推荐
- Spring Cloud Zuul 2(基于配置中心的动态API网关)
在大体了解了API Zuul 和 配置中心Config后我们来尝试完成一个基于配置中心的动态API网关 创建项目 命名为api-gateway-dynamic-route并加入config 和 Zuu ...
- 分布式基础学习【二】 —— 分布式计算系统(Map/Reduce)
二. 分布式计算(Map/Reduce) 分布式式计算,同样是一个宽泛的概念,在这里,它狭义的指代,按Google Map/Reduce框架所设计的分布式框架.在Hadoop中,分布式文件系统,很大程 ...
- SpringBoot20 集成SpringSecurity02 -> 利用SpringSecurity进行前后端分离的登录验证
1 SpirngBoot环境搭建 创建一个SpringBoot项目即可,详情参见三少的相关博文 参考博文 -> 点击前往 SpirngBoot项目脚手架 -> 点击前往 2 引入Spirn ...
- opencv3读取视频并保存为图片
#include <iostream> #include <vector> #include <opencv2/opencv.hpp> using namespac ...
- css 样式文字溢出显示省略号
在table中使用溢出样式,table样式要设置为”table-layout: fixed“,即<table style="table-layout: fixed;"> ...
- linux内核被加载的过程
二,linux内核被加载的过程 一,linux安装时遇到的概念解析 内核必须模块vmlinz(5M左右)不认识硬盘,原本是需要写跟loader中一样的内容,来加载非必要模块. 内核非必要的功能被编译为 ...
- 2014年Linux 和开源技术回顾盘点
ZDNet科技观察家StevenJ.Vaughan-Nichols在年终发表了对Linux和开源技术这一年跌宕起伏的总结,细数这一年中的惊喜和不堪. 2014Linux之殇 “心脏出血(Heartbl ...
- 特征选择Boruta
A good feature subset is one that: contains features highly correlated with (predictive of) the clas ...
- 在aspx页面中使用三元表达式
第一种使用方法:判断GridView绑定的数据是否为空 用GridView或其他控件绑定数据的时候,有时候需要判断从数据库中获取的值是否是空值,然后显示相应的内容,如果在后置代码中写的话只有是在Row ...
- SQL语句性能分析常用命令
DBCC freeproccache DBCC dropcleanbuffers 1.set statistics IO {ON| OFF} /*Transact-SQL 语句生成的磁盘活动量的信息* ...