VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 一个简洁实用的PHP缓存类完整实例

这篇文章主要介绍了一个简洁实用的PHP缓存类完整实例,缓存的应用对于php大型项目的开发来说至关重要,需要的朋友可以参考下

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

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

  1. <?php 
  2. /* 
  3. * 缓存类 cache 
  4. */ 
  5. class cache { 
  6. //缓存目录 
  7. var $cacheRoot = "./cache/"; 
  8. //缓存更新时间秒数,0为不缓存 
  9. var $cacheLimitTime = 0; 
  10. //缓存文件名 
  11. var $cacheFileName = ""; 
  12. //缓存扩展名 
  13. var $cacheFileExt = "php"; 
  14. /* 
  15.   * 构造函数 
  16.   * int $cacheLimitTime 缓存更新时间 
  17.   */ 
  18. function cache( $cacheLimitTime ) { 
  19.   if( intval( $cacheLimitTime ) ) 
  20.   $this->cacheLimitTime = $cacheLimitTime; 
  21.   $this->cacheFileName = $this->getCacheFileName(); 
  22.   ob_start(); 
  23. }  
  24. /* 
  25.   * 检查缓存文件是否在设置更新时间之内 
  26.   * 返回:如果在更新时间之内则返回文件内容,反之则返回失败 
  27.   */ 
  28. function cacheCheck(){ 
  29.   if( file_exists( $this->cacheFileName ) ) { 
  30.   $cTime = $this->getFileCreateTime( $this->cacheFileName ); 
  31.   if( $cTime + $this->cacheLimitTime > time() ) { 
  32.   echo file_get_contents( $this->cacheFileName ); 
  33.   ob_end_flush(); 
  34.   exit; 
  35.   } 
  36.   } 
  37.   return false; 
  38. } 
  39. /* 
  40.   * 缓存文件或者输出静态 
  41.   * string $staticFileName 静态文件名(含相对路径) 
  42.   */ 
  43. function caching( $staticFileName = "" ){ 
  44.   if( $this->cacheFileName ) { 
  45.   $cacheContent = ob_get_contents(); 
  46.   ob_end_flush(); 
  47.   if( $staticFileName ) { 
  48.   $this->saveFile( $staticFileName, $cacheContent ); 
  49.   } 
  50.   if( $this->cacheLimitTime ) 
  51.   $this->saveFile( $this->cacheFileName, $cacheContent ); 
  52.   } 
  53. }  
  54. /* 
  55.   * 清除缓存文件 
  56.   * string $fileName 指定文件名(含函数)或者all(全部) 
  57.   * 返回:清除成功返回true,反之返回false 
  58.   */ 
  59. function clearCache( $fileName = "all" ) { 
  60.   if( $fileName != "all" ) { 
  61.   $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt; 
  62.   if( file_exists( $fileName ) ) { 
  63.   return @unlink( $fileName ); 
  64.   }else return false; 
  65.   } 
  66.   if ( is_dir( $this->cacheRoot ) ) { 
  67.   if ( $dir = @opendir( $this->cacheRoot ) ) { 
  68.   while ( $file = @readdir( $dir ) ) { 
  69.   $check = is_dir( $file ); 
  70.   if ( !$check ) 
  71.   @unlink( $this->cacheRoot . $file ); 
  72.   } 
  73.   @closedir( $dir ); 
  74.   return true; 
  75.   }else{ 
  76.   return false; 
  77.   } 
  78.   }else{ 
  79.   return false; 
  80.   } 
  81. } 
  82. /*根据当前动态文件生成缓存文件名*/ 
  83. function getCacheFileName() { 
  84.   return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt; 
  85. } 
  86. /* 
  87.   * 缓存文件建立时间 
  88.   * string $fileName 缓存文件名(含相对路径) 
  89.   * 返回:文件生成时间秒数,文件不存在返回0 
  90.   */ 
  91. function getFileCreateTime( $fileName ) { 
  92.   if( ! trim($fileName) ) return 0; 
  93.   if( file_exists( $fileName ) ) { 
  94.   return intval(filemtime( $fileName )); 
  95.   }else return 0; 
  96. }  
  97. /* 
  98.   * 保存文件 
  99.   * string $fileName 文件名(含相对路径) 
  100.   * string $text 文件内容 
  101.   * 返回:成功返回ture,失败返回false 
  102.   */ 
  103. function saveFile($fileName, $text) { 
  104.   if( ! $fileName || ! $text ) return false; 
  105.   if( $this->makeDir( dirname( $fileName ) ) ) { 
  106.   if( $fp = fopen( $fileName, "w" ) ) { 
  107.   if( @fwrite( $fp, $text ) ) { 
  108.   fclose($fp); 
  109.   return true; 
  110.   }else { 
  111.   fclose($fp); 
  112.   return false; 
  113.   } 
  114.   } 
  115.   } 
  116.   return false; 
  117. } 
  118. /* 
  119.   * 连续建目录 
  120.   * string $dir 目录字符串 
  121.   * int $mode 权限数字 
  122.   * 返回:顺利创建或者全部已建返回true,其它方式返回false 
  123.   */ 
  124. function makeDir( $dir, $mode = "0777" ) { 
  125.   if( ! $dir ) return 0; 
  126.   $dir = str_replace( "\\", "/", $dir ); 
  127.   $mdir = ""; 
  128.   foreach( explode( "/", $dir ) as $val ) { 
  129.   $mdir .= $val."/"; 
  130.   if( $val == ".." || $val == "." || trim( $val ) == "" ) continue; 
  131.   if( ! file_exists( $mdir ) ) { 
  132.   if(!@mkdir( $mdir, $mode )){ 
  133.   return false; 
  134.   } 
  135.   } 
  136.   } 
  137.   return true; 
  138. } 
  139. } 
  140. ?> 

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

  1. include( "cache.php" ); 
  2. $cache = new cache(30); 
  3. $cache->cacheCheck(); 
  4. echo date("Y-m-d H:i:s"); 
  5. $cache->caching(); 
  6.  



出处:http://www.phpfensi.com/php/20210325/14001.html


相关教程