php的页面缓存练习
<?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的页面缓存练习的更多相关文章
- 探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)
其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- cache-contro页面缓存处理设置
<meta http-equiv="pragma" content="no-cache">,pragma与no-cache用于定义页面缓存,不缓存页 ...
- webform的页面缓存
给页面添加<%@ OutputCache Duration="10" VaryByParam="*"%>标签就可以启用页面缓存. Duration表 ...
- 【WP开发】正确理解页面缓存
注:本文内容面向Runtime App. 在新建项目后,细心观察,你会发现在App类中有以下代码: // TODO: 将此值更改为适合您的应用程序的缓存大小 rootFrame.CacheSize = ...
- [转]MVC3缓存之一:使用页面缓存
本文转自:http://www.cnblogs.com/parry/archive/2011/03/19/OutputCache_In_MVC3.html 在以前的WebForm的开发中,在页面的头部 ...
- [转]Asp.net mvc 网站之速度优化 -- 页面缓存
网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...
- Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- ASP.NET 页面缓存
ASP.NET 实现页面缓存页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam ...
- MVC3缓存之一:使用页面缓存
MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中,在View的Hom ...
随机推荐
- javascript模式——Mixin
Mixin是一种扩展收集功能的方式,能提高代码的复用率. 在javascript中,原型可以继承于其它对象的原型,并且可以为任意数量的实例定义属性.可以利用这一点来促进函数的复用. 下面一段代码就是将 ...
- asp.net几种<% %>用法
在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
创建当前时间 NSDate *date = [NSDate date]; 从现在开始的24小时 NSTimeInterval a_day = 24*60*60; NSDate *tomorrow = ...
- memcached的安装和linux下memcached服务自启动的配置
关于memcached在windows和linux环境的安装,以及在Linux系统系memcached服务自启动的配置,可以参考我在csdn上下的博客, windows和linux环境下memcach ...
- Spring2.0集成Quartz1.5.2调度框架
Quartz是个开放源码项目,提供了丰富的作业调度集.希望您在阅读完本文并看过代码演示后,可以把Quartz的基本特性应用到任何Java™应用程序中.现代的Web应用程序框架在范围和复杂性方面都有所发 ...
- 如果有需要确解MD5的,可以尝试这个网站。
http://www.hashkiller.co.uk/md5-decrypter.aspx
- mudOS配置
# 本泥巴的名称name : new夕阳再现# 定义用来接受玩家连线的端口#单端口 port number : XXXX#双端口如下:external_port_1 : telnet 10010ext ...
- PHP获取IP信息
<?php /** * 获取客户端IP * @param integer $type 返回类型 0:string,1:long * @return string|long */ function ...
- struts2笔记12-声明式异常
1.配置异常处理 <action name="save" class="com.test.actions.ProductAction" method=&q ...
- chroot
用途:更改命令的根目录. 语法:chroot Directory Command 描述: 注意:如果新根目录中的特殊文件具有与实际根目录不同的主要和次要设备号,则可能会覆盖文件系统. 只有具有 roo ...