<?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. uva 1471 Defense Lines

    题意: 给一个长度为n(n <= 200000) 的序列,你删除一段连续的子序列,使得剩下的序列拼接起来,有一个最长的连续递增子序列 分析: 就是最长上升子序列的变形.需要加一个类似二分搜索就好 ...

  2. Asp.Net长文件名下载的问题和解决办法

    在Asp.Net中写了一个附件上传和下载的程序,附件上传到数据库中,然后将附件的GUID保存起来,我们可以根据GUID来找到数据库中的附件,一般附件下载的代码是: <!--<br /> ...

  3. IIS应用程序池自动回收问题的有效解决办法

    问题:Timer不能持续执行,如果长时间没有访问,就会被IIs自动回收.造成一些定时作业无法完成. 解决方式1:改用windows服务或是winform方式 解决方式2:在Application_En ...

  4. (七)Android中AIDL的应用与理解

    一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...

  5. html css float left与 float right的使用说明

    点评: CSS中很多时候会用到浮动来布局,也就是经常见到的float:left或者float:right,简单点来说,前者是左浮动(往左侧向前边的非浮动元素飘,全是飘得元素的话,就按照流式来浮动从左到 ...

  6. 关于Application Cache

    http://blog.csdn.net/fwwdn/article/details/8082433 http://www.cnblogs.com/blackbird/archive/2012/06/ ...

  7. Today See>

    http://wenku.baidu.com/view/b08f3575f46527d3240ce061.html http://wenku.baidu.com/view/a3419558be2348 ...

  8. ASP 代码当前记录集不支持更新问题的解决办法。

    错误类型: ADODB.Recordset (0x800A0CB3) 当前记录集不支持更新.这可能是提供程序的限制,也可能是选定锁定类型的限制. /Model/manage/Admin_Admin.a ...

  9. java代码收藏:获取HttpServletRequest中某一前缀的参数

    public static Map getParametersStartingWith(ServletRequest request, String prefix) { Enumeration par ...

  10. java.lang.OutOfMemoryError: PermGen space 解决方案

    只需两步: 将值改为512或者1024,然后CTRL+S,重启tomcat 和eclipse即可.