PHP文件缓存类实现代码


Posted in PHP onOctober 26, 2015

php中缓存分类数据库缓存,文件缓存内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考。
页面缓存类
代码如下 :

<?php  
/*include( "cache.php" );  
  
$cache = new cache(30);  
$cache->cacheCheck();  
  
echo date("Y-m-d H:i:s");  
  
$cache->caching(); */
class cache {  
 //缓存目录  
 var $cacheRoot    = "./cache/";  
 //缓存更新时间秒数,0为不缓存  
 var $cacheLimitTime  = 3; 
 //缓存文件名  
 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();  
   //echo $cacheContent;  
   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;  
 }  
}  
?>

上面使用算是页面缓存了,每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
给大家介绍一个Memcache缓存,算是内存缓存。
代码如下

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)n";
$get_result = $memcache->get('key');
echo "Data from the cache:n";
var_dump($get_result);
?>

Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。

以上就是本文的全部内容,希望对大家学习php缓存有所帮助。

PHP 相关文章推荐
快速配置PHPMyAdmin方法
Jun 05 PHP
基于HTTP长连接的&quot;服务器推&quot;技术的php 简易聊天室
Oct 31 PHP
php设计模式之命令模式的应用详解
May 21 PHP
PHP数据库万能引擎类adodb配置使用以及实例集锦
Jun 12 PHP
完美的2个php检测字符串是否是utf-8编码函数分享
Jul 28 PHP
PHP命令行脚本接收传入参数的三种方式
Aug 20 PHP
php自定义apk安装包实例
Oct 20 PHP
php上传大文件失败的原因及应对策略
Oct 20 PHP
PHP实现的mysql主从数据库状态检测功能示例
Jul 20 PHP
PHP递归统计系统中代码行数
Sep 19 PHP
Mac下关于PHP环境和扩展的安装详解
Oct 17 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
Nov 24 PHP
php多线程实现方法及用法实例详解
Oct 26 #PHP
浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
Oct 26 #PHP
PHP和C#可共用的可逆加密算法详解
Oct 26 #PHP
日常整理PHP中简单的图形处理(经典)
Oct 26 #PHP
php 参数过滤、数据过滤详解
Oct 26 #PHP
php解析url并得到url中的参数及获取url参数的四种方式
Oct 26 #PHP
php实现CSV文件导入和导出
Oct 24 #PHP
You might like
hadoop中一些常用的命令介绍
2013/06/19 PHP
解决nginx不支持thinkphp中pathinfo的问题
2015/07/21 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
2020/03/23 PHP
实用javaScript技术-屏蔽类
2006/08/15 Javascript
&amp;lt;script defer&amp;gt; defer 是什么意思
2009/05/10 Javascript
简单的jquery拖拽排序效果实现代码
2011/09/20 Javascript
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
2013/04/15 Javascript
js中继承的几种用法总结(apply,call,prototype)
2013/12/26 Javascript
代码获取历史上的今天发生的事
2014/04/11 Javascript
浅谈js数组和splice的用法
2016/12/04 Javascript
限时抢购-倒计时的完整实例(分享)
2017/09/17 Javascript
JavaScript实现简单动态进度条效果
2018/04/06 Javascript
Vue项目中添加锁屏功能实现思路
2018/06/29 Javascript
Vue常见面试题整理【值得收藏】
2018/09/20 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
用Python中的字典来处理索引统计的方法
2015/05/05 Python
Python 常用的安装Module方式汇总
2017/05/06 Python
python学习教程之Numpy和Pandas的使用
2017/09/11 Python
Python面向对象之继承代码详解
2018/01/29 Python
tensorflow 获取模型所有参数总和数量的方法
2018/06/14 Python
python 对txt中每行内容进行批量替换的方法
2018/07/11 Python
pytorch使用Variable实现线性回归
2019/05/21 Python
python采集百度搜索结果带有特定URL的链接代码实例
2019/08/30 Python
pytorch 指定gpu训练与多gpu并行训练示例
2019/12/31 Python
matplotlib教程——强大的python作图工具库
2020/10/15 Python
AmazeUI 模态窗口的实现代码
2020/08/18 HTML / CSS
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
饲料采购员岗位职责
2013/12/19 职场文书
买房子个人收入证明
2014/01/16 职场文书
营销与策划专业求职信
2014/06/20 职场文书
工程造价专业求职信
2014/07/17 职场文书
班主任师德师风自我剖析材料
2014/10/02 职场文书
2015年酒店服务员工作总结
2015/05/18 职场文书
小时代观后感
2015/06/10 职场文书
教你用python实现12306余票查询
2021/06/30 Python
Python极值整数的边界探讨分析
2021/09/15 Python