PHP中常見的緩衝技術執行個體分析_php技巧

來源:互聯網
上載者:User

本文執行個體分析了PHP中常見的緩衝技術。分享給大家供大家參考。具體如下:

JBLOG在開發的過程中,對效能的最佳化做了不少工作。為了盡量減少不必要的資料庫查詢,我對一些資料進行了緩衝和靜態化處理。

緩衝的原理:把一些經常要用到但又很少改動的資料以數組或其它形式儲存到一個獨立的PHP檔案中,然後在需要用到的時候包含進來。

緩衝的優點:能夠大大減少資料庫的查詢次數,減輕資料庫的壓力,提高程式的執行效率。

JBLOG緩衝的資料有:系統設定、部落格分類、側欄最新日誌、最新評論、部落格統計、日誌歸檔、友情連結、標籤等。通過緩衝這些資料,執行一次頁面資料庫的查詢次數從十幾次減少到3次。

JBLOG中與緩衝相關的函數儲存在include目錄下的cache.func.php裡,主要函數:

//重新整理緩衝function recache($cachestr = '') {if (!$cachestr) { $cachelist = array('config','class','archive','newcomment','newpost','link','tag','statistic','topblog');} else { $cachelist = explode(',',$cachestr); foreach ($cachelist as $cache) {  $cachename = $cache.'_recache';  if (function_exists($cachename)) {  $cachename();  } }}}

recache()函數用來重新整理緩衝,每一個緩衝以獨立的函數存在,重新整理緩衝時僅需執行一次相應的函數即可。

//將字串寫進檔案function writeToFile($cachename,$content = '') {$allowcache = $cachelist = array('config','class','archive','newcomment','newpost','link','tag','statistic','topblog');if (in_array($cachename,$allowcache)) { $cache_dir = JBLOG_ROOT.'cache_data/'; $cache_file_name = $cache_dir.'cache_'.$cachename.'.php'; if (!is_dir($cache_dir)) {  @mkdir($cache_dir,0777); } if ($fp = @fopen($cache_file_name,'wb')) {  $content = "<?php\r\n//該檔案是系統自動產生的快取檔案,請勿修改\r\n//建立時間:".date('Y-m-d H:i:s',time())."\r\n\r\nif (!defined('IN_JBLOG')) {exit('Access Denied!');}\r\n\r\n".$content."\r\n\r\n?>";  @fwrite($fp,$content);  @fclose();  @chmod($cache_file_name,0777); } else {  echo '快取檔案<b>'.$cache_dir.$cache_file_name.'</b>建立失敗!<br />'; }} else { die('緩衝名稱<b>'.$cachename.'</b>不在系統允許的範圍內!');}}

writeToFile() 函數用於將資料寫入到緩衝目錄下以cache_緩衝名稱.php命名的檔案中。
再看具體的緩衝函數執行個體:

//緩衝部落格分類function class_recache() {global $db,$tablepre;$content = '';$sql = "SELECT id,classname,description,orderid,arcnum FROM `{$tablepre}class` ORDER BY orderid";$result = $db->query($sql);while ($row = $db->fetch_array($result)) { $content .= "\tarray(\r\n"; $content .= "\t'id'=>'".addslashes($row['id'])."',\r\n"; $content .= "\t'classname'=>'".addslashes($row['classname'])."',\r\n"; $content .= "\t'description'=>'".addslashes($row['description'])."',\r\n"; $content .= "\t'orderid'=>'".addslashes($row['orderid'])."',\r\n"; $content .= "\t'arcnum'=>'".addslashes($row['arcnum'])."',\r\n"; $content .= "\t),\r\n";}$content = substr($content,0,strrpos($content,','));$content = "\$class_cache = array(\r\n{$content}\r\n);";writeToFile('class',$content);}

class_recache()函數從資料庫取出資料,然後構造一個數組,以分類ID作為數組的索引,類別的資訊作為對應的值,方便資料的訪問。
緩衝的引入:

所有快取資料統一在include目錄下的common.inc.php引入,代碼如下:

//載入系統設定資訊,檔案不存在則重建緩衝if ([email=!@include(JBLOG_ROOT.]!@include(JBLOG_ROOT.'cache_data/cache_config.php'[/email])) {require_once(JBLOG_ROOT.'include/cache.func.php');recache('config');exit('成功建立系統配置資訊緩衝,請重新整理頁面!');}//載入緩衝,快取檔案不存在則重建緩衝$cachestr = '';$cachelist = array('class','archive','newcomment','newpost','link','tag','statistic','topblog');foreach ($cachelist as $cachename) {$cachestr .= (@include(JBLOG_ROOT.'cache_data/cache_'.$cachename.'.php')) ? '' : $cachename.',';}$cachestr = substr($cachestr,0,strrpos($cachestr,','));if ($cachestr) {require_once(JBLOG_ROOT.'include/cache.func.php');recache($cachestr);exit('所有緩衝重建完成,請重新整理頁面!');}unset($cachelist,$cachename,$cachestr);

先載入配置資訊是因為,在建立其它快取檔案的時候,經常要用到系統的設定資訊,如系統設定中有一個選項可以讓使用者自訂最新日誌的數量,在緩衝最新日誌的時候就會用到該變數,所以必須先確保配置資訊成功緩衝後,再緩衝其它項目。

希望本文所述對大家的php程式設計有所協助。

聯繫我們

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