2008
11.21

WordPress 使用 Memcached

WordPress 除了 PHP 使用 Wp Super Cache 外掛產生靜態頁面,也可以採用 Memcached 來幫忙加速。Memcache 本身分為 Server / Client 兩種,一個是 Memcached ,而 Client 則是各種語言的實做,對 PHP5 而言
php5-memcache 就是它的 Client Library 。

安裝方式

wajig install memcached
wajig install php5-memcache

接著修改 Memcache Server 的設定,建議使用預設值就可以了。

vi /etc/memcached.conf

裡面有幾個設定,分別是 -d (用daemon方式執行), -v (顯示輸出), -vv (顯示更多輸出), -m 64 (用64M記憶體),
-p 11211(使用 port 11211), -u root (用 root 身份來跑), -l 127.0.0.1 (監聽的IP/介面) , -c 1024 (限制連線數目), -k (鎖定分頁記憶體), -M (當記憶體耗盡的時候回報錯誤)。

在 /etc/php5/apache2/php.ini 最下面可以看到 wajig 幫忙寫好的設定

extension=memcache.so
[memcache]
memcache.dbpath=/var/lib/memcache
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0

沒特別需要不用去更改,接著重新啟動 Server 即可!但這樣還不能用,因為雖然裝了 php5-memcache ,但還得在程式中實做才行!

PHP5 透過 PHP Memcache Client Library 實做的方法可以參考 在 Windows 上安裝 Memcached ,介紹的非常完整,至於 WordPress 的話,則是有人實做了 Objkect-Cache(感謝 Shami 的部落格 介紹),安裝方法是先下載object-cache.php,然後上傳到 wp-content 目錄下即可自動啟用。

這篇文章參考了:
Ubuntu下 讓 WordPress 使用 Memcached 緩存快取加速
如何使用 memcached 做快取 – 推薦!
MySQL UDF (User-defined function) 與 memcached

效能增加:在非登入狀態下,載入首頁用 get_num_queries() 算共有個 27 個 Query ,使用 memcached 之後, IE 6 減少到 6 個,Firefox 也是而 IE 7 則是減少到 8 個。(第一次載入查詢次數會比較多,接著因為快取住了,所以會減少)

UPDATE: UPDATE: 如果載入 object-cache.php 後系統錯誤,說 addServer() 只支援傳入六個參數的話,請打開 object-cache.php 找到第 342 行,修改成為:

$this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15), true, array($this, 'failure_callback'));
/* 改為 */
$this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15);
/* 因為 Memcache::addServer 是這樣的: *
/ bool Memcache::addServer(string host[, int port [, bool persistent [, int weight [, int timeout [, int retry_interval]]]]])

1 comment so far

Add Your Comment
  1. 有点意思 学习了哈

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>