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远程连接MYSQL数据库非常慢的解决方法
Jul 05 PHP
php面向对象全攻略 (九)访问类型
Sep 30 PHP
判断是否为指定长度内字符串的php函数
Feb 16 PHP
PHP类中Static方法效率测试代码
Oct 17 PHP
基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍
Apr 22 PHP
用php代码限制国内IP访问我们网站
Sep 26 PHP
基于命令行执行带参数的php脚本并取得参数的方法
Jan 25 PHP
PHP的PDO操作简单示例
Mar 30 PHP
PHP MSSQL 分页实例
Apr 13 PHP
php关闭warning问题的解决方法
May 17 PHP
PHP 观察者模式深入理解与应用分析
Sep 25 PHP
PhpStorm 2020.3:新增开箱即用的PHP 8属性(推荐)
Oct 30 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实现批量删除挂马文件及批量替换页面内容完整实例
2016/07/08 PHP
Laravel网站打开速度优化的方法汇总
2017/07/16 PHP
Laravel如何创建服务器提供者实例代码
2019/04/15 PHP
基于jquery的表格排序
2010/09/11 Javascript
JavaScript 用Node.js写Shell脚本[译]
2012/09/20 Javascript
js实现简单随机抽奖的方法
2015/01/27 Javascript
javascript针对不确定函数的执行方法
2015/12/16 Javascript
Javascript实现基本运算器
2017/07/15 Javascript
js 索引下标之li集合绑定点击事件
2018/01/12 Javascript
关于Angularjs中跨域设置白名单问题
2018/04/17 Javascript
JavaScript设计模式之策略模式实现原理详解
2020/05/29 Javascript
jQuery编写QQ简易聊天框
2020/08/27 jQuery
python中的__slots__使用示例
2015/02/26 Python
初步介绍Python中的pydoc模块和distutils模块
2015/04/13 Python
在Python中操作文件之seek()方法的使用教程
2015/05/24 Python
Python实现代码统计工具(终极篇)
2016/07/04 Python
Python中交换两个元素的实现方法
2018/06/29 Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
2019/11/19 Python
Python实现使用dir获取类的方法列表
2019/12/24 Python
Python函数的定义方式与函数参数问题实例分析
2019/12/26 Python
把vgg-face.mat权重迁移到pytorch模型示例
2019/12/27 Python
Python如何基于smtplib发不同格式的邮件
2019/12/30 Python
linux 下python多线程递归复制文件夹及文件夹中的文件
2020/01/02 Python
详解Python3中的 input() 函数
2020/03/18 Python
python爬虫中url管理器去重操作实例
2020/11/30 Python
详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式
2021/01/24 Python
使用phonegap查找联系人的实现方法
2017/03/31 HTML / CSS
俄罗斯卫浴采暖及维修用品超级市场:Dkrussia
2020/05/12 全球购物
大学生如何写自荐信
2014/01/08 职场文书
表彰会主持词
2014/03/26 职场文书
心得体会格式及范文
2016/01/25 职场文书
如何写好一份优秀的工作总结?
2019/06/21 职场文书
导游词之河姆渡遗址博物馆
2019/10/10 职场文书
pytorch中的model=model.to(device)使用说明
2021/05/24 Python
Consul在linux环境的集群部署
2022/04/08 Servers
python数字图像处理之对比度与亮度调整示例
2022/06/28 Python