<?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. 在Activity中动态设置TextView的隐藏属性

    if (true) { //显示 viewHolder.tvLine.setVisibility(View.INVISIBLE);} else { //不显示 viewHolder.tvLine.se ...

  2. winform使用xml作为数据源

    1.新建窗体应用程序 2.拖放DataGridView 3.在bin\Debug中放入XML文件 using System; using System.Collections.Generic; usi ...

  3. mysqli连接数据库函数

    <?php $mysqli=@new mysqli("localhost", "root", "123456", "xsph ...

  4. php cli模式没有加载php.ini

    这两天在虚拟机的linux里编译安装了php,同时也把swoole的扩展也编译上了.在/etc/php.ini里加上了extension=swoole.so,但是用php -m 查看加载的模块并没有s ...

  5. PHP文件访问技术

    <?php $file=fopen("test.txt","r"); //以只读方式打开test.txt $char=fgetc($file); echo ...

  6. HTTP中的URL长度限制(资料整理)

    HTTP中的URL长度限制   首先,其实http 1.1 协议中对url的长度是不受限制的,协议原文: The HTTP protocol does not place any a priori l ...

  7. 算法分析-动态规划(cut_rod)

    什么是动态规划,我们要如何描述它? 动态规划算法通常基于一个递推公式及一个或多个初始状态. 当前子问题的解将由上一次子问题的解推出.使用动态规划来解题只需要多项式时间复杂度, 因此它比回溯法.暴力法等 ...

  8. 提交(post)xml文件给指定url的2种方法

    原文:提交(post)xml文件给指定url的2种方法 1  这段代码是在网上搜到的,拿来共享,项目正好要用到.其中的data你只需要传递一个xml字符串就可以 protected   string  ...

  9. Android的logcat命令详解

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  10. Android UI SurfaceView的使用-绘制组合图型,并使其移动

    绘制容器类: //图形绘制容器 public class Contanier { private List<Contanier> list; private float x=0,y=0; ...