国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP文件頁面緩存類

2024-09-04 11:44:08
字體:
來源:轉載
供稿:網友

在php中緩存分類數據庫緩存,文件緩存和內存緩存,下面我來給各位同學詳細介紹PHP文件緩存類實現代碼,有需要了解的朋友可參考.

一個不錯的PHP文件頁面緩存類代碼如下:

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

上面使用算是頁面緩存了,每次訪問頁面的時候,都會先檢測相應的緩存頁面文件是否存在,如果不存在,就連接數據庫,得到數據,顯示頁面并同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發揮作用了,模板引擎和網上常見的一些緩存類通常有此功能.

給大家介紹一個Memcache緩存了,算是內存緩存了,代碼如下:

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

Memcached是高性能的,分布式的內存對象緩存系統,用于在動態應用中減少數據庫負載,提升訪問速度.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝山区| 开远市| 左权县| 高邮市| 神农架林区| 泰顺县| 义马市| 墨玉县| 杭锦后旗| 和顺县| 鹿邑县| 仙桃市| 莱州市| 新乐市| 仁化县| 揭东县| 长子县| 昆山市| 孝昌县| 江西省| 三亚市| 丹棱县| 林芝县| 昭觉县| 青铜峡市| 德令哈市| 临西县| 贞丰县| 阳江市| 乌兰县| 郸城县| 灵丘县| 南乐县| 台南市| 永顺县| 尤溪县| 塔河县| 汾西县| 永年县| 太保市| 烟台市|