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 相关文章推荐
遍历指定目录下的所有目录和文件的php代码
Nov 27 PHP
php将mysql数据库整库导出生成sql文件的具体实现
Jan 08 PHP
PHP框架Swoole定时器Timer特性分析
Aug 19 PHP
CentOS 安装 PHP5.5+Redis+XDebug+Nginx+MySQL全纪录
Mar 25 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 PHP
降低PHP Redis内存占用
Mar 23 PHP
php array_chunk()函数用法与注意事项
Jul 12 PHP
PHP7数组的底层实现示例
Aug 25 PHP
Yii框架日志操作图文与实例详解
Sep 09 PHP
php输出文字乱码的解决方法
Oct 04 PHP
在laravel中使用with实现动态添加where条件
Oct 10 PHP
TP5框架实现自定义分页样式的方法示例
Apr 05 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
谈谈新手如何学习PHP 默默经典版本
2009/08/04 PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
2014/09/22 PHP
网页自动跳转代码收集
2009/09/27 Javascript
JavaScript replace(rgExp,fn)正则替换的用法
2010/03/04 Javascript
js自定义事件代码说明
2011/01/31 Javascript
javascript学习笔记(三) String 字符串类型介绍
2012/06/19 Javascript
用Jquery实现滚动新闻
2014/02/12 Javascript
JS删除字符串中重复字符方法
2014/03/09 Javascript
JS实现清除指定cookies的方法
2014/09/20 Javascript
JS控制伪元素的方法汇总
2016/04/06 Javascript
javascript日期比较方法实例分析
2016/06/17 Javascript
jQuery图片瀑布流的简单实现代码
2017/03/15 Javascript
jQuery表单验证之密码确认
2017/05/22 jQuery
基于JavaScript实现多级菜单效果
2017/07/25 Javascript
浅谈Node.js之异步流控制
2017/10/25 Javascript
js回文数的4种判断方法示例
2019/06/04 Javascript
Java 生成随机字符的示例代码
2021/01/13 Javascript
[01:07:02]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD BO3 第三场 2月26日
2021/03/11 DOTA
python中的随机函数random的用法示例
2018/01/27 Python
分享Pycharm中一些不为人知的技巧
2018/04/03 Python
python list是否包含另一个list所有元素的实例
2018/05/04 Python
pytorch多进程加速及代码优化方法
2019/08/19 Python
scikit-learn线性回归,多元回归,多项式回归的实现
2019/08/29 Python
Python合并2个字典成1个新字典的方法(9种)
2019/12/19 Python
三个python爬虫项目实例代码
2019/12/28 Python
HTML5 语音搜索只需一句代码
2013/01/03 HTML / CSS
美国最大的香水出口:FragranceX.com
2017/11/04 全球购物
Swisse官方海外旗舰店:澳大利亚销量领先,自然健康品牌
2017/12/15 全球购物
泰国在线书店:SE-ED
2020/06/21 全球购物
大学生优秀团员事迹材料
2014/01/30 职场文书
电子工程专业毕业生求职信
2014/03/14 职场文书
党员自我剖析材料(群众路线)
2014/10/06 职场文书
导游词书写之黄山
2019/08/06 职场文书
《合作意向书》怎么写?
2019/08/20 职场文书
python实现调用摄像头并拍照发邮箱
2021/04/27 Python
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python