PHP通過檔案儲存體來實現緩衝,

來源:互聯網
上載者:User

PHP通過檔案儲存體來實現緩衝,

PHP通過檔案儲存體來實現緩衝在一些資料庫資料記錄較大,但是伺服器有限的時候,可能一條MySQL查詢就會好幾百毫秒,一個簡單的頁面一般也有十幾條查詢,這個時候也個頁面載入下來基本要好幾秒了,如果並發量高的話伺服器基本就癱瘓了,造成一個頁面很久也載入不下來,這個時候我們可以使用檔案快取來緩解下MySQL的壓力,下面給個使用例子。 

[php] view plaincopy
  1. <?php  
  2.   
  3. //頁面商務邏輯處理,獲得結果  
  4. $objPage = new Page_IndexModel($arrParams);  
  5.   
  6. //一系列的商務邏輯放在了objPage中,調用process方法獲得結果集  
  7. $arrResult = $objPage->process();  
  8.   
  9. //獲得結果後smarty賦值  
  10. $smarty->assign($arrResult);  
  11.   
  12. //輸出模板  
  13. $smarty->display();  
  14.   
  15. ?>  


現在我們用檔案快取來略過Page業務處理這一步 
[php] view plaincopy
  1. <?php  
  2.   
  3. $cachFile = './index.php';  
  4. //快取檔案存在且時間不超過一小時,則直接使用緩衝的結果集,不在進行任何的MySQL查詢了  
  5. if(file_exists($cacheFile) && time()-filemtime($cachFile) < 3600) {  
  6.    //使用緩衝中的結果  
  7.    $arrResult = include($cachFile);  
  8. } else {  
  9.    $objPage = new Page_IndexModel($arrParams);  
  10.    $arrResult = $objPage->process();  
  11.    $strContent = "<?php \n return ".var_export($arrResult, true)."\n;";  
  12.      
  13.    //將結果集緩衝  
  14.    file_put_contents($cachFile, $strContent);  
  15. }  
  16.   
  17. //獲得結果後smarty賦值  
  18. $smarty->assign($arrResult);  
  19.   
  20. //輸出模板  
  21. $smarty->display();  


參考來源: 
PHP通過檔案儲存體來實現緩衝
http://www.lai18.com/content/407149.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.