php专区

 首页 > php专区 > PHP应用 > php类库 > PHP文件页面缓存类 - php类库

PHP文件页面缓存类 - php类库

分享到:
【字体:
导读:
          在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考.一个不错的PHP文件页面缓存类代码如下:?php/**缓存类cach...

PHP文件页面缓存类

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

一个不错的PHP文件页面缓存类代码如下:

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

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

给大家介绍一个Memcache缓存了,算是内存缓存了,代码如下:

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

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

分享到:
php网页病毒清除办法 - php类库
php网页病毒清除办法 你的网页是不是经常被无故的在php,asp,html,js 等文件后台加上一些木马地址呢?我以前有个站就是这样,所以一恨之下写了这段代码,文章虽然有一点浪费资源了,但是总比我们手动清除要好吧,下面我为讲讲程序的清除病毒的原理吧. 首先们要读取 $checkFile 文件这个文章是判断...
PHP ZIP压缩类代码 - php类库
PHP ZIP压缩类代码 本程序可以快速的实现把我们的文件利用php压缩类压缩成我们想的zip,或者rar 的压缩包,后缀名可以自定义,压缩算法是来自国外一个网站抄的. 首先实例化,然后传参,两个参数,第一个关于你文件地址的一个Array,第二个是要你要保存的压缩包文件的绝对地址. For example,代码如...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……