一个简洁实用的PHP缓存类完整实例


Posted in PHP onJuly 26, 2014

本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内、清除缓存文件、根据当前动态文件生成缓存文件名、连续创建目录、缓存文件输出静态等功能。对于采用PHP开发CMS系统来说,离不开对缓存的处理,合理利用好缓存可有效的提高程序执行效率。

php缓存类文件完整代码如下:

<?php
/*
* 缓存类 cache
*/
class cache {
//缓存目录
var $cacheRoot = "./cache/";
//缓存更新时间秒数,0为不缓存
var $cacheLimitTime = 0;
//缓存文件名
var $cacheFileName = "";
//缓存扩展名
var $cacheFileExt = "php";
/*
  * 构造函数
  * int $cacheLimitTime 缓存更新时间
  */
function cache( $cacheLimitTime ) {
  if( intval( $cacheLimitTime ) )
  $this->cacheLimitTime = $cacheLimitTime;
  $this->cacheFileName = $this->getCacheFileName();
  ob_start();
} 
/*
  * 检查缓存文件是否在设置更新时间之内
  * 返回:如果在更新时间之内则返回文件内容,反之则返回失败
  */
function cacheCheck(){
  if( file_exists( $this->cacheFileName ) ) {
  $cTime = $this->getFileCreateTime( $this->cacheFileName );
  if( $cTime + $this->cacheLimitTime > time() ) {
  echo file_get_contents( $this->cacheFileName );
  ob_end_flush();
  exit;
  }
  }
  return false;
}
/*
  * 缓存文件或者输出静态
  * string $staticFileName 静态文件名(含相对路径)
  */
function caching( $staticFileName = "" ){
  if( $this->cacheFileName ) {
  $cacheContent = ob_get_contents();
  ob_end_flush();
  if( $staticFileName ) {
  $this->saveFile( $staticFileName, $cacheContent );
  }
  if( $this->cacheLimitTime )
  $this->saveFile( $this->cacheFileName, $cacheContent );
  }
} 
/*
  * 清除缓存文件
  * string $fileName 指定文件名(含函数)或者all(全部)
  * 返回:清除成功返回true,反之返回false
  */
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 ( is_dir( $this->cacheRoot ) ) {
  if ( $dir = @opendir( $this->cacheRoot ) ) {
  while ( $file = @readdir( $dir ) ) {
  $check = is_dir( $file );
  if ( !$check )
  @unlink( $this->cacheRoot . $file );
  }
  @closedir( $dir );
  return true;
  }else{
  return false;
  }
  }else{
  return false;
  }
}
/*根据当前动态文件生成缓存文件名*/
function getCacheFileName() {
  return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
}
/*
  * 缓存文件建立时间
  * string $fileName 缓存文件名(含相对路径)
  * 返回:文件生成时间秒数,文件不存在返回0
  */
function getFileCreateTime( $fileName ) {
  if( ! trim($fileName) ) return 0;
  if( file_exists( $fileName ) ) {
  return intval(filemtime( $fileName ));
  }else return 0;
} 
/*
  * 保存文件
  * string $fileName 文件名(含相对路径)
  * string $text 文件内容
  * 返回:成功返回ture,失败返回false
  */
function saveFile($fileName, $text) {
  if( ! $fileName || ! $text ) return false;
  if( $this->makeDir( dirname( $fileName ) ) ) {
  if( $fp = fopen( $fileName, "w" ) ) {
  if( @fwrite( $fp, $text ) ) {
  fclose($fp);
  return true;
  }else {
  fclose($fp);
  return false;
  }
  }
  }
  return false;
}
/*
  * 连续建目录
  * string $dir 目录字符串
  * int $mode 权限数字
  * 返回:顺利创建或者全部已建返回true,其它方式返回false
  */
function makeDir( $dir, $mode = "0777" ) {
  if( ! $dir ) return 0;
  $dir = str_replace( "\\", "/", $dir );
  $mdir = "";
  foreach( explode( "/", $dir ) as $val ) {
  $mdir .= $val."/";
  if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
  if( ! file_exists( $mdir ) ) {
  if(!@mkdir( $mdir, $mode )){
  return false;
  }
  }
  }
  return true;
}
}
?>

使用该缓存类的时候可将以上代码保存为cache.php,具体用法如下所示:

include( "cache.php" );
$cache = new cache(30);
$cache->cacheCheck();
echo date("Y-m-d H:i:s");
$cache->caching();
PHP 相关文章推荐
php生成随机数或者字符串的代码
Sep 05 PHP
解析php中heredoc的使用方法
Jun 17 PHP
php实现MD5加密16位(不要默认的32位)
Aug 12 PHP
CodeIgniter生成网站sitemap地图的方法
Nov 13 PHP
ThinkPHP3.1数据CURD操作快速入门
Jun 19 PHP
实现在同一方法中获取当前方法中新赋值的session值解决方法
Jun 26 PHP
php画图实例
Nov 05 PHP
php实现在限定区域里自动调整字体大小的类实例
Apr 02 PHP
Yii2.0中的COOKIE和SESSION用法
Aug 12 PHP
layui框架实现文件上传及TP3.2.3(thinkPHP)对上传文件进行后台处理操作示例
May 12 PHP
PHP实现打包zip并下载功能
Jun 12 PHP
laravel 使用auth编写登录的方法
Sep 30 PHP
PHP实现多图片上传类实例
Jul 26 #PHP
PHP判断文章里是否有图片的简单方法
Jul 26 #PHP
php中创建和调用webservice接口示例
Jul 25 #PHP
Codeigniter中mkdir创建目录遇到权限问题和解决方法
Jul 25 #PHP
codeigniter上传图片不能正确识别图片类型问题解决方法
Jul 25 #PHP
2个Codeigniter文件批量上传控制器写法例子
Jul 25 #PHP
CodeIgniter框架数据库事务处理的设计缺陷和解决方案
Jul 25 #PHP
You might like
全国FM电台频率大全 - 22 重庆市
2020/03/11 无线电
PHP禁止页面缓存的代码
2011/10/23 PHP
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
2013/07/05 PHP
php实现mysql备份恢复分卷处理的方法
2014/12/26 PHP
CI操作cookie的方法分析(基于helper类库)
2016/03/28 PHP
PHP使用Redis实现防止大并发下二次写入的方法
2017/10/09 PHP
jQuery获取css z-index在各种浏览器中的返回值
2010/09/15 Javascript
js 利用image对象实现图片的预加载提高访问速度
2013/03/29 Javascript
JS取文本框中最小值的简单实例
2013/11/29 Javascript
seajs中模块的解析规则详解和模块使用总结
2014/03/12 Javascript
原生js的弹出层且其内的窗口居中
2014/05/14 Javascript
$(document).ready(function() {})不执行初始化脚本
2014/06/19 Javascript
JavaScript实现向OL列表内动态添加LI元素的方法
2015/03/21 Javascript
jQuery如何获取动态添加的元素
2016/06/24 Javascript
AngularJS  $on、$emit和$broadcast的使用
2016/09/05 Javascript
基于jQuery的select下拉框选择触发事件实例分析
2016/11/18 Javascript
Mongoose学习全面理解(推荐)
2017/01/21 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
Nodejs+express+ejs简单使用实例代码
2017/09/18 NodeJs
JS异步错误捕获的一些事小结
2019/04/26 Javascript
[03:17]2014DOTA2 国际邀请赛中国区预选赛 四强专访
2014/05/23 DOTA
[47:35]VP vs Pain 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/20 DOTA
python实现识别手写数字 python图像识别算法
2020/03/23 Python
对python中的iter()函数与next()函数详解
2018/10/18 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
Python图像读写方法对比
2020/11/16 Python
字中字效果的实现【html5实例】
2016/05/03 HTML / CSS
澳大利亚当地社区首选的光学商店:1001 Optical
2019/08/24 全球购物
中式餐厅创业计划书范文
2014/01/23 职场文书
清华大学自主招生自荐信
2014/01/29 职场文书
业绩考核岗位职责
2014/02/01 职场文书
学生会离职感言
2014/02/11 职场文书
2014年入党积极分子学习三中全会思想汇报
2014/09/13 职场文书
推荐信范文大全
2015/03/27 职场文书
奠基仪式致辞
2015/07/30 职场文书
Java实现多线程聊天室
2021/06/26 Java/Android