使用Xcache緩衝器加速你的PHP網站

來源:互聯網
上載者:User

  由於國內網站備案比較麻煩,所以筆者便把網站放到了香港。雖然網站免去了備案的困擾,但是從訪問速度上來看,一般要比放於國內的網站慢2-3倍,所以便想辦法對網站做了一些簡單的最佳化,比如使用緩衝系統來提升網站頁面訪問速度。

  目前用於Web的緩衝系統很多,包括squid、varnish、Nginx內建的proxy_cache、FastCGI中的fastcgi_cache、APC、Xcache等。

  像squid、varnish、Nginx內建的proxy_cache這類系統,屬於重量級產品,配置維護比較麻煩,不適合小型網站,而且一般用這類系統緩衝靜態內容,比片、css、JavaScript等;像FastCGI中的fastcgi_cache,它主要用於緩衝動態內容,所以在訪問使用fastcgi_cache的網站時速度極快,但是筆者使用時發現其維護比較麻煩,特別是每次網站有資料要更新後,如果不等到緩衝期到期後得需要手動清除緩衝才能看到網站更新的內容;至於APC個人感覺效能就一般了,拿它和Xcache比較時發現訪問使用Xcache網站的速度明顯高於使用APC網站的速度(筆者沒有具體測試),所以最終選擇了使用Xcache。

  我們都知道PHP是一種動態語言,它在執行時是以解釋的方式執行,所以PHP代碼每次執行時都會被解析和轉換成作業碼(opcode)。而Xcache是一個開源的作業碼緩衝器/最佳化器,它通過把解析/轉換PHP後的作業碼緩衝到檔案(直到原始代碼被修改)從而避免重複的解析過程,提高了代碼的執行速度,通常能夠提高頁面產生速率2-5倍,降低了伺服器負載,提高了使用者訪問網站的速度。

 

一、安裝Xcache

1 # wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz2 # tar zxvf xcache-1.3.0.tar.gz3 # cd xcache-1.3.04 # /usr/local/php/bin/phpize5 # ./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer--with-php-config=/usr/local/php/bin/php-config6 # make && make install

  註:--enable-xcache表示啟用Xcache支援;--enable-xcache-coverager表示包含用於測量加速器功效的附加特性;--enable-xcache-optimizer表示啟用作業碼最佳化

  安裝完畢後系統會提示xcache.so模組產生路徑,本次產生路徑為/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/,然後把xcache.so移動到/usr/local/php/include/php/ext目錄下。

 

二、組態管理Xcache

1、修改php設定檔

  配置時我們可以參考xcache的配置模板xcache.ini,此檔案位於Xcache安裝程式中

# vi /usr/local/php/lib/php.ini

  然後添加如下內容

 1 extension_dir=/usr/local/php/include/php/ext 2  3 [xcache-common] 4 extension = xcache.so 5 [xcache.admin] 6 xcache.admin.enable_auth = On 7 xcache.admin.user = "xcache" 8 xcache.admin.pass = "" 9 10 [xcache]11 xcache.shm_scheme ="mmap"12 xcache.size=60M13 xcache.count =114 xcache.slots =8K15 xcache.ttl=016 xcache.gc_interval =017 xcache.var_size=4M18 xcache.var_count =119 xcache.var_slots =8K20 xcache.var_ttl=021 xcache.var_maxttl=022 xcache.var_gc_interval =30023 xcache.test =Off24 xcache.readonly_protection = On25 xcache.mmap_path ="/tmp/xcache"26 xcache.coredump_directory =""27 xcache.cacher =On28 xcache.stat=On29 xcache.optimizer =Off30 31 [xcache.coverager]32 xcache.coverager =On33 xcache.coveragedump_directory =""

 

2、產生Xcache快取檔案

# touch /tmp/xcache# chmod 777 /tmp/xcache

3、產生Xcache管理員的秘密(MD5密文)

# echo -n "123456" | 

md5sume10adc3949ba59abbe56e057f20f883e

  然後將上述產生的MD5密文粘貼到php.ini檔案中xcache.admin.pass = ""選項,xcache.admin.pass= "e10adc3949ba59abbe56e057f20f883e"

4、拷貝Xcache管理程式到網站根目錄下

# cp -a /tmp/xcache-1.3.0/admin//usr/local/nginx/html/

  然後重新啟動PHP,然後訪問http://localhost/admin ,使用者名稱為xcache 密碼為123456;另外,還可以通過phpinfo來驗證PHP是否支援Xcache

這裡要注意的一點就是Xcache只能緩衝預設的一些對象,如int, string, array等,不能緩衝對象,否則讀取的時候就會報錯。

  如果你非要緩衝對象的話也有辦法就是將對象序列化,讀取的時候再還原序列化一次。

  下面我寫的一個Xcache的簡單類:

 

程式碼

 1 <?php 2 /** 3 * Xcache moudle 4 */ 5 class cacheHelper{ 6   public $prefix; 7   function __construct(){ 8     if(!function_exists('xcache_get')){ 9       exit("This application must required XCache module.");10     }11   }12   /**13    * __set14    *15    * @param mixed $name16    * @param mixed $value17    * @access public18    * @return void19    */20   public function __set($name, $value){21     xcache_set($this->prefix.$name, $value);22   }23   /**24    * __get25    *26    * @param mixed $name27    * @access public28    * @return mixed29    */30   public function __get($name){31     return xcache_get($this->prefix.$name);32   }33   /**34    * __isset35    *36    * @param mixed $name37    * @access public38    * @return bool39    */40   public function __isset($name){41     return xcache_isset($this->prefix.$name);42   }43   /**44    * __unset45    *46    * @param mixed $name47    * @access public48    * @return void49    */50   public function __unset($name){51     xcache_unset($this->prefix.$name);52   }53 }54 ?>

 

聯繫我們

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