• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. php頁面緩存實現方法

        時間:2024-10-09 01:58:45 PHP 我要投稿
        • 相關推薦

        php頁面緩存實現方法

          php頁面緩存實現方法可能很多PHP新手都不是很了解,下面給大家介紹php頁面緩存實現方法,一起來了解一下吧!

          php頁面緩存實現方法

          在php頁面緩存主要用到的是ob系列函數,如ob_start(),ob_end_flush(),ob_get_contents(),但是更高級的緩存是不使用這些函數的,本文章最后一個實現就有講到,大家可參考一下。

          ob_start():頁面緩存開始的標志,此函數一下的內容直至ob_end_flush()或者ob_end_clean()都保存在頁面緩存中;

          ob_get_contents():用來獲取頁面緩存中的內容,獲取到以后呢,我們就可以想怎么處理這些內容都行了,過濾字段啦,匹配內容啦,都可以~~~ :)

          ob_end_flush():表示頁面緩存結束。并且經我驗證,緩存的內容將輸出到當前頁面上,也就是可以顯示緩存內容。

          用此三個php函數,就可以實現強大的功能。如果數據庫查詢量較大,可以用cache來解決這個問題。

          下面是編碼部分。

          1.初始化函數,一般是設置頁面緩存路徑、緩存文件命名格式等,可按個人喜好自定義。這里用到的識別ID是經加密的$_SERVER[REQUEST_URI]參數。這個函數中最后還有一個if判斷:若未過緩存期,則加載緩存文件,否則加載源文件。

          代碼如下

          function page_init()

          {

          $url = $_SERVER['REQUEST_URI'];//子url,該參數一般是唯一的

          $pageid = md5($url);

          $dir = str_replace('/','_',substr($_SERVER['SCRIPT_NAME'],1,-4));

          //目錄命名方式,如exp_index

          if(!file_exists($pd = PAGE_PATH.$dir.'/'))@mkdir($pd,0777) or die("$pd目錄創建失敗");

          //如cache/page/exp_index/

          define('PAGE_FILE',$pd.$pageid.'.html');

          //如cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html

          $contents = file_get_contents(PAGE_FILE);//讀出

          if($contents && substr($contents, 13, 10) > time() )//對應page_cache()函數中加上的.自定義頭部

          {

          echo substr($contents, 27);

          exit(0);

          }

          return true;

          }

          2.頁面緩存函數,這里使用到一個技巧:在緩存文件的內容中加上一個頭部信息--過期時間,所以每次只需要對頭部中的過期時間和當前時間進行比較(在page_init()函數中進行)就能判斷緩存是否過期了。

          代碼如下

          function page_cache($ttl = 0)

          {

          $ttl = $ttl ? $ttl : PAGE_TTL;//緩存時間,默認3600s

          $contents = ob_get_contents();//從緩存中獲取內容

          $contents = "<!--page_ttl:".(time() + $ttl)."-->n".$contents;

          //加上自定義頭部:過期時間=生成時間+緩存時間

          file_put_contents(PAGE_FILE, $contents);//寫入緩存文件中

          ob_end_flush();//釋放緩存

          }

          3.函數使用,注意這兩個函數有先后執行順序,還有別忘了ob_start()

          代碼如下

          <?php

          page_init();//頁面緩存初始化

          ob_start();//開啟緩存

          ...//代碼段

          page_cache(60);//一般是最后一行

          ?>

          例2

          下面做個示例來說明PHP頁面緩存技術:

          代碼如下

          <?php

          $_time =10;

          $dir="D:php";

          function cache_start($_time, $dir)

          {

          $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';

          $cachetime = $_time;

          ob_start();

          if(file_exists($cachefile) && (time()-filemtime($cachefile) < $cachetime))

          {

          include($cachefile);

          ob_end_flush();

          exit;

          }

          }

          function cache_end($dir)

          {

          $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';

          $fp = fopen($cachefile, 'w');

          fwrite($fp, ob_get_contents());

          fclose($fp);

          ob_end_flush();

          }

          cache_start($_time, $dir);

          //以下是輸出的內容,放在cache_start和cache_end兩個方法之間

          for ($i=0;$i<5;$i++)

          {

          echo $i;

          sleep(1);

          }

          cache_end($dir);

          ?>

          例

          利用生成文件做緩存

          代碼如下

          <?php

          ob_start();

          /**

          * @author 何名慧

          * @copyright 2009-3-13

          * @param string $cache_folder 緩文件夾

          * @param int $cache_create_time 文件緩存時間

          * @example $cache=new Esj_Cache('./_cache',100)

          * @example $cache->read_cache() 讀取緩存并輸出

          * @example $cache->creatre_cache() 創建緩存文件(放在文件未尾)

          * @example $cache->list_file() 返回所有緩存文件列表

          * @example $cache->del_file() 刪除所有緩存文件

          */

          class Esj_Cache{

          private $cache_folder=null;//cacher文件夾

          private $wroot_dir=null;//站點目錄

          private $cacher_create_time=null;//cacher文件的建立時間

          public function __construct($cache_foldername,$cacher_time=100)

          {

          ob_start();

          $this->wroot_dir=$_SERVER['DOCUMENT_ROOT'];

          $this->cache_folder=$cache_foldername;

          $this->cacher_create_time=$cacher_time;

          }

          public function read_cache()

          {

          try {

          if(self::create_folder($this->cache_folder))

          {

          self::get_cache();//輸出緩存文件信息

          }else

          {

          echo "緩存文件夾創建失敗!";

          return false;

          }

          }catch(Exception $e){

          echo $e;

          return false;

          }

          }

          //測試緩存文件夾是否存在

          private function exist_folder($foler)

          {

          if(file_exists($this->wroot_dir."/".$foler)){

          return true;

          }else {

          return false;

          }

          }

          //建立一個新的文件夾

          private function create_folder($foler)

          {

          if(!self::exist_folder($foler))

          {

          try{

          mkdir($this->wroot_dir."/".$foler,0777);

          chmod($this->wroot_dir."/".$foler,0777);

          return true;

          }catch (Exception $e)

          {

          self::get_cache();//輸出緩存

          return false;

          }

          return false;

          }

          else

          {

          return true;

          }

          }

          //讀取緩存文件

          private function get_cache()

          {

          $file_name=self::get_filename();

          if (file_exists($file_name)&&((filemtime($file_name)+$this->cacher_create_time) > time()))

          {

          $content=file_get_contents($file_name);

          if($content)

          {

          echo $content;

          ob_end_flush();

          exit;

          }else

          {

          echo "文件讀取失敗";

          exit;

          }

          }

          }

          //返回文件的名字

          private function get_filename()

          {

          $filename=$file_name=$this->wroot_dir.'/'.$this->cache_folder.'/'.md5($_SERVER['QUERY_STRING']).".html";

          return $filename;

          }

          //建立緩存文件

          public function create_cache()

          {

          $filename=self::get_filename();

          if($filename!="")

          {

          try{

          file_put_contents($filename,ob_get_contents());

          return true;

          }catch (Exception $e)

          {

          echo "寫緩存失敗:".$e;

          exit();

          }

          return true;

          }

          }

          // 取得緩存中的所有文件

          public function list_file()

          {

          $path=$this->cache_folder;

          if ($handle = opendir($path)) {

          while (false !== ($file = readdir($handle))) {

          if($file!="." && $file!="..") {

          $path1=$path."/".$file;

          if(file_exists($path1))

          {

          $result[]=$file;

          }

          }

          }

          closedir($handle);

          }

          return $result;

          }

          //刪除緩存中的所有文件

          public function del_file()

          {

          $path=$this->cache_folder;

          if ($handle = opendir($path)) {

          while (false !== ($file = readdir($handle))) {

          if($file!="." && $file!="..") {

          $path1=$path."/".$file;

          if(file_exists($path1))

          {

          unlink($path1);

          }

          }

          }

          closedir($handle);

          }

          return true;

          }

          }

          ?>


        【php頁面緩存實現方法】相關文章:

        關于php堆排序實現原理與應用方法10-09

        將php實現過濾UBB代碼09-11

        探討PHP函數的實現原理及性能07-07

        PHP錯誤類型及屏蔽方法08-22

        實現硬盤raid的方法07-31

        PHP處理密碼的幾種方法10-17

        十天學會php的方法09-23

        MongoDB的PHP驅動方法與技巧大全09-08

        了解PHP環境搭建與配置的方法08-03

        PHP開發環境安裝的方法有哪些10-27

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 视频一区二视频一区二区 | 亚洲综合中文字幕在线专区 | 中文字字幕在线精品乱码 | 欧美亚洲尤物久久综合精品 | 亚洲人成网a在线播放 | 综合久久久综合欧美98 |