<?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. Android学习之Notification

    Notification可以在手机的状态栏发出一则通知,它需要用NotificationManager来管理,实现Notification其实很简单. 1.通过getsystemservice方法获得 ...

  2. Java中两种实现多线程方式的对比分析

    本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...

  3. 记一次phpStudy apache启动后自动关闭 修改过程

    第一种可能原因:路径包含中文 .添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文 ...

  4. hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup

    一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...

  5. php将unicode编码转为utf-8方法

    介绍 在前端开发中,为了让中文在不同的环境下都能很好的显示,一般是将中文转化为unicode格式,即\u4f60,比如:"你好啊"的 unicode编码为"\u4f60\ ...

  6. VASP 软件在ubuntu10.04下的安装过程

    开始三步按照http://www.cnblogs.com/baby-lee进行. 安装完intel fortran composer后,需要把ifort命令写进.bashrc文件. 4.添加ifort ...

  7. DWZ 框架remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题

    关于jquery  remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题.可以直接修改tomcat 配置文件server.xml  设置 URIEncoding=utf-8属性,将ge ...

  8. mysql性能优化学习笔记(4)索引的优化

    一.选择合适的索引列     1.在where,group by,order by,on从句中出现的列     2.索引字段越小越好(因为数据库的存储单位是页,一页中能存下的数据越多越好 )      ...

  9. leetcode算法刷题(二)——动态规划(一)

    上次刷了五六道题,都是关于string处理的,这次想换个知识点刷一下,然后再回头刷string的题,当做复习.. 这几天主要会选择动态规划的题目,因为以前从没刷过这方面的东西,很多东西都不是很懂..就 ...

  10. windows后台服务程序编写

    Windows后台服务程序编写 1. 为什么要编写后台服务程序 工作中有一个程序需要写成后台服务的形式,摸索了一下,跟大家分享. 在windows操作系统中后台进程被称为 service. 服务是一种 ...