PHP 中的資料庫緩衝原理

來源:互聯網
上載者:User

PHP 中的資料庫緩衝原理


如果後台應用接收到瀏覽器端的查詢請求後,每次都與資料庫連接讀取資料,勢必增加資料庫的負擔。而往往有大量的請求是重複的,我們可以把這些重複的資訊採用緩衝技術儲存下來,重複使用,這樣,在某些情況下可以大大提高程式的效能。
  1,緩衝函數
  cache_write函數接受$string參數,寫到$file檔案中。注意var_export函數,作用是:
  此函數返回關於傳遞給該函數的變數的結構資訊,它和 var_dump() 類似,不同的是其返回的表示是合法的 PHP 代碼。您可以通過將函數的第二個參數設定為 TRUE,從而返回變數的表示。
  這些參數可以是數組或常量,而這些數組或常量通常為從資料庫中取出的記錄,或非序列化(unserialize)對象後得到的資料。這些都可以緩衝到本地的文字檔中。
  cache_write函數很簡單,需要讀取資料時,先判斷緩衝是否存在,存在的話就不去串連資料庫取資料,而是直接讀出緩衝的文字檔,直接產生了數組或常量等類型的資料,可以直接使用。
[php]
  <?php
  //檔案名稱func.inc.php
  define("CACHEDIR", "./");  //定義快取檔案夾
  function cache_write($file, $string, $type = 'array')
  {
   if(is_array($string))
   {
    $type = strtolower($type);
    if($type == 'array')
    {
     $string = "<?phpn return ".var_export($string,TRUE).";n?>";
    }
    elseif($type == 'constant')
    {
     $data='';
     foreach($string as $key => $value)
       $data .= "define('".strtoupper($key)."','".addslashes($value)."');n";
     $string = "<?phpn".$data."n?>";
    }
   }
   $strlen = file_put_contents(CACHEDIR.$file, $string);
   chmod(CACHEDIR.$file, 0777);
   return $strlen;
  }
  function cache_read($file)
  {
   $cachefile = CACHEDIR.$file;
   if(!file_exists($cachefile))
     return array();
   return include $cachefile;
  }
  function cache_delete($file)
  {
   return @unlink(CACHEDIR.$file);
  }
  if(!function_exists('file_put_contents'))
  {
   define('FILE_APPEND', 8);
   function file_put_contents($file, $string, $append = '')
   {
    $mode = $append == '' ? 'wb' : 'ab';
    $fp = @fopen($file, $mode) or exit("Can not open file $file !");
    flock($fp, LOCK_EX);
    $stringlen = @fwrite($fp, $string);
    flock($fp, LOCK_UN);
    @fclose($fp);
    return $stringlen;
   }
  }
  ?>
[/php]
  2,寫緩衝和讀取的樣本
[php]
    <?php
        //寫緩衝
      include "func.inc.php";
    
      $arr = array (1, 2, array ("a", "b", "c"));
      cache_write('test.cache.php', $arr);  //快取檔案 test.cache.php
      ?>
[/php]
[php]
    <?php
        //讀緩衝
      include "func.inc.php";
    
      $var = cache_read('test.cache.php');
      print_r($new_var);
    
      print_r($var);
    
      foreach ($var as $k=>$v)
      {
        echo '<br>' . $k . '=' . $v ;
      }
      ?>
[/php]
    3,效能分析
    緩衝之所以能提高效能,是通過本地磁碟空間換網路存取速度和資料庫伺服器存取時間的結果。
    a = 本機讀寫時間
    b = 本機佔用空間
    c = 網路傳輸時間
    d = 資料庫伺服器磁碟時間
    可以估算到,如果資料庫與應用程式存在於一台機器時,主要是 a 與 d 的比較,效果可能並不明顯,甚至還要糟一點。因為資料庫系統針對磁碟存取經過了精心最佳化,是作業系統對檔案的普通讀寫無法相比的。
    如果原生磁碟存取效率不佳,有時從區域網路的資料庫上取得資料,可能比從原生緩衝取資料還快,這種情況比較少見。而隨著請求數量的大量增加,緩衝的效果就會明顯起來。
覺得很不錯,所以推薦一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.