Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動網站的速度。


 
 
 
 
安裝Memcached
# yum -y install memcached
 
啟動Memcached
# service memcached start
# ss -tnl | grep 11211
 
安裝Memcached的PHP擴展
 
安裝Memcached
# tar xf memcache-2.2.7.tgz
# cd memcache-2.2.7
/usr/local/php/bin/phpize
# ./configure –with-php-config=/usr/local/php/bin/php-config –enable-memcache
# make && make install
 
上述安裝完後會有類似以下的提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
 
編輯/etc/php.ini,在「動態模塊」相關的位置添加如下一行來載入memcache擴展:
# vim /etc/php.ini
 
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
 
而後對memcached功能進行測試,在網站目錄中建立測試頁面test.php,添加如下內容:

connect("127.0.0.1", 11211) or die("Could not connect");
 
$version = $mem->getVersion();
echo "Server's version: ".$version."
\n";
 
$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)
\n";
 
$get_result = $mem->get('hellokey');
echo "$get_result is from memcached server.";
?>


 
 
 
 
簡易的Memcache 安裝腳本
wget -c http://soft.vpser.net/lib/libevent/libevent-2.0.13-stable.tar.gz
tar zxvf libevent-2.0.13-stable.tar.gz
cd libevent-2.0.13-stable/
./configure –prefix=/usr/local/libevent
make&& make install
cd ../
 
echo “/usr/local/libevent/lib/" >> /etc/ld.so.conf
ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /lib/libevent-2.0.so.5
ldconfig
 
wget -c http://soft.vpser.net/web/memcached/memcached-1.4.7.tar.gz
tar zxvf memcached-1.4.7.tar.gz
cd memcached-1.4.7/
./configure –prefix=/usr/local/memcached
make &&make install
cd ../
 
ln /usr/local/memcached/bin/memcached /usr/bin/memcached
 
cp conf/memcached-init /etc/init.d/memcached
chmod +x /etc/init.d/memcached
useradd -s /sbin/nologin nobody
 
if [ -s /etc/debian_version ]; then
update-rc.d -f memcached defaults
elif [ -s /etc/redhat-release ]; then
chkconfig –level 345 memcached on
fi
 
echo “Copy Memcached PHP Test file…"
cp conf/memcached.php /home/wwwroot/memcached.php
 
echo “Starting Memcached…"
/etc/init.d/memcached start
編輯/etc/php.ini,在「動態模塊」相關的位置添加如下一行來載入memcache擴展
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
ERIC6269_1

Tags: , ,