<?php
/*
* 自定义页面缓存类
*/
namespace page_cache;
class Page
{
public $CacheRoot = "pageCache/";//缓存文件存放目录,
public $CacheLimitTime = 0;//缓存文件更新时间,0不更新
public $CacheFileName = '';//缓存文件名,
public $CacheFileExt = '.php';//缓存文件扩展名称 //构造函数,设置缓存更新时间
public function __construct($CacheLimitTime)
{
if(intval($CacheLimitTime))
{
$this->CacheLimitTime = $CacheLimitTime;
$this->CacheFileName = $this->GetFileName();
ob_start();
return true;
}
else
{
return false;
}
} //检查缓存文件是否在更新时间内,在更新时间内返回文件内容,不在更新时间内返回false
public function CheckTime()
{
if(file_exists($this->CacheFileName))
{
if($this->CacheLimitTime +$this->GetFileCreateTime($this->CacheFileName)> time())
{
echo file_get_contents($this->CacheFileName);
ob_end_flush();//输出内容到缓冲区
exit();
}
}
return false;
} /*
* 创建缓存文件
* param $cacheFileName; type string; value 自定义缓存文件的名称
*/
public function CreateCacheFile($cacheFileName='')
{
$getCacheContent = ob_get_contents();
ob_end_flush ();
if(!empty($cacheFileName))
{
return $this->SaveFile($cacheFileName,$getCacheContent);
}
else
{
return $this->SaveFile($this->CacheFileName,$getCacheContent);
}
} /*
* 清除缓存
* param $fileName;type string;value all;note:value is all,clear all cache;
*/
public function ClearCache($fileName = "all")
{
if($fileName!='all')
{
$fileName = $this->CacheRoot.strtoupper(md5($fileName)).$this->CacheFileExt;
if(file_exists($fileName))
{
return @unlink($fileName);
}
else
{
return false;
}
}
if($fileName = 'all')
{
if(is_dir(($this->CacheRoot)))
{
if($dir = opendir($this->CacheRoot))
{
while($file = readdir($dir))
{
if(!is_dir($file))
{
unlink($this->CacheRoot.$file);
} }
closedir($dir);
return true;
}
}
else
{
return false;
}
}
}
//获取缓存的文件名称
public function GetFileName()
{
return $this->CacheRoot.strtoupper(md5($_SERVER['REQUEST_URI'])).$this->CacheFileExt;
} //返回缓存文件上次修改的时间
public function GetFileCreateTime($filename)
{
if(!trim($filename)) return 0;
if(file_exists($filename))
{
return intval(filemtime($filename));
}
else
{
return 0;
}
} //保存文件并写入内容
public function SaveFile($filename,$text)
{
if(empty($filename)||empty($text))
{
return false;
}
if($this->CacheDir(dirname($filename)))
{
if($fp = fopen($filename,"w"))
{
if(fwrite($fp,$text))
{
fclose($fp);
return true;
}
else
{
fclose($fp);
return false;
}
}
} } //创建缓存目录
public function CacheDir($dir,$mode="0777")
{
if(empty($dir))
{
return false;
}
$dir = str_replace("\\","/",$dir); $dir = explode("/",$dir); $mkdir = '';
foreach($dir as $val)
{
$mkdir .= $val.'/';
if($val=='./'||$val=='../'||$val=="")
{
continue;
}
if(!file_exists($mkdir))
{
if(!@mkdir($mkdir,$mode))
{
return false;
}
}
} return true; } } $cache = new Page(30);
$cache->CheckTime();
echo "Now is : " . date ( "Y-m-d H:i:s" ,time());
$cache ->CreateCacheFile();

php的页面缓存练习的更多相关文章

  1. 探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)

    其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...

  2. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  3. cache-contro页面缓存处理设置

    <meta http-equiv="pragma" content="no-cache">,pragma与no-cache用于定义页面缓存,不缓存页 ...

  4. webform的页面缓存

    给页面添加<%@ OutputCache Duration="10" VaryByParam="*"%>标签就可以启用页面缓存. Duration表 ...

  5. 【WP开发】正确理解页面缓存

    注:本文内容面向Runtime App. 在新建项目后,细心观察,你会发现在App类中有以下代码: // TODO: 将此值更改为适合您的应用程序的缓存大小 rootFrame.CacheSize = ...

  6. [转]MVC3缓存之一:使用页面缓存

    本文转自:http://www.cnblogs.com/parry/archive/2011/03/19/OutputCache_In_MVC3.html 在以前的WebForm的开发中,在页面的头部 ...

  7. [转]Asp.net mvc 网站之速度优化 -- 页面缓存

    网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...

  8. Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  9. ASP.NET 页面缓存

    ASP.NET 实现页面缓存页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam ...

  10. MVC3缓存之一:使用页面缓存

    MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中,在View的Hom ...

随机推荐

  1. random background

    function roll(){ var bg = document.getElementById("loginbg"); var rnd = Math.floor(Math.ra ...

  2. Knime 使用 初探

    使用数据挖掘工具 Knime,分析某公司用户使用情况. 首先,打开csv文件数据,看到有以下门类: 时间.track id 歌曲名.用户行为.用户id.日期.snap_id 即歌曲门类 然后,打开Kn ...

  3. PhoneGap Xcode iOS教程

    http://mobile.51cto.com/web-334924.htmhttp://phonegap.com/install/http://www.phonegap100.com/jiaoche ...

  4. 使用XmlReader读Xml

    XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...

  5. HDU 1004 - Let the Balloon Rise(map 用法样例)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  6. .offset()与.position()区别

    jQuery中有两个获取元素位置的方法offset()和position(),两者的定义如下:   offset():获取匹配元素在当前视口的相对偏移.返回的对象包含两个整形属性:top 和 left ...

  7. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  8. Oracle导出空表处理方法

    exp或是expdp命令在导出数据的时候会把表记录数为0的表过滤掉,无法导出.通过如下方法可以导出记录数0的表.   1.先查询一下哪些表是空的: select table_name from use ...

  9. Android 为应用添加数字角标

    今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道an ...

  10. Ubuntu之修改用户名和主机名

    记得曾几何时,想把自己电脑的“乌班兔儿”取个响亮的名字,但是问了很久度娘和谷哥,都要我把当前用户删除了(userdel -r xxx),重新建一个用户(adduser xxx),但是,我的电脑是所有环 ...