web2.0和web1.0不一樣,web1.0以內容為中心,所以web1.0做負載只需緩衝內容就可以了,使用反向 Proxy快取頁面面就可以解決大部分問題了。而web2.0鼓勵使用者互動,內容都是動態,只做反向 Proxy命的話中率低,而且對資料庫進行頻繁的寫,對資料庫壓力大。所以,web2.0對web1.0更需要使用記憶體緩衝。
memcached高效能的,分布式的記憶體對象緩衝系統,在Live App中減少資料庫負載,提升訪問速度。memcached已經被廣泛應用在各種軟體中,例如,nginx、mysql
一、服務端安裝
使用weget下載libevent和memcached
libevent: http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
memcached: http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
1.先安裝libevent Java代碼
- tar zxvf libevent-1.4.9-stable.tar.gz
- cd libevent-1.4.9-stable
- ./configure
- make
- make install
tar zxvf libevent-1.4.9-stable.tar.gz cd libevent-1.4.9-stable./configuremakemake install
2.安裝memcached Java代碼
- tar zxvf memcached-1.2.6.tar.gz
- cd memcached-1.2.6
- ./configure --enable-threads
- make
- make install
tar zxvf memcached-1.2.6.tar.gzcd memcached-1.2.6./configure --enable-threadsmakemake install
註:如果啟動時出現“memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open
shared object file: No such file or directory”之類的資訊,表示memcached找不到libevent的位置
所以,請先使用whereis libevent得到位置,然後串連到memcached所尋找的路徑 Java代碼
- [root@localhost tools]# whereis libevent
- libevent: /usr/local/lib/libevent.la /usr/local/lib/libevent.so /usr/local/lib/libevent.a
- [root@localhost tools]# LD_DEBUG=libs memcached -v 2>&1 > /dev/null | less
- 3848: find library=libevent-1.4.so.2 [0]; searching
- 3848: search cache=/etc/ld.so.cache
- 3848: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/
- usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64
- (system search path)
- 3848: trying file=/lib64/tls/x86_64/libevent-1.4.so.2
- 3848: trying file=/lib64/tls/libevent-1.4.so.2
- 3848: trying file=/lib64/x86_64/libevent-1.4.so.2
- 3848: trying file=/lib64/libevent-1.4.so.2
- 3848:
- 3848: find library=libc.so.6 [0]; searching
- 3848: search cache=/etc/ld.so.cache
- 3848: trying file=/lib64/libc.so.6
- 3848:
- 3848: find library=libnsl.so.1 [0]; searching
- 3848: search cache=/etc/ld.so.cache
- 3848: trying file=/lib64/libnsl.so.1
- 3848:
- 3848: find library=librt.so.1 [0]; searching
- 3848: search cache=/etc/ld.so.cache
- 3848: trying file=/lib64/librt.so.1
- 3848:
- 3848: find library=libresolv.so.2 [0]; searching
- 3848: search cache=/etc/ld.so.cache
- [root@localhost tools]# ln -s /usr/local/lib/libevent-1.4.so.2 /lib64/
[root@localhost tools]# whereis libeventlibevent: /usr/local/lib/libevent.la /usr/local/lib/libevent.so /usr/local/lib/libevent.a[root@localhost tools]# LD_DEBUG=libs memcached -v 2>&1 > /dev/null | less 3848: find library=libevent-1.4.so.2 [0]; searching 3848: search cache=/etc/ld.so.cache 3848: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path) 3848: trying file=/lib64/tls/x86_64/libevent-1.4.so.2 3848: trying file=/lib64/tls/libevent-1.4.so.2 3848: trying file=/lib64/x86_64/libevent-1.4.so.2 3848: trying file=/lib64/libevent-1.4.so.2 3848: 3848: find library=libc.so.6 [0]; searching 3848: search cache=/etc/ld.so.cache 3848: trying file=/lib64/libc.so.6 3848: 3848: find library=libnsl.so.1 [0]; searching 3848: search cache=/etc/ld.so.cache 3848: trying file=/lib64/libnsl.so.1 3848: 3848: find library=librt.so.1 [0]; searching 3848: search cache=/etc/ld.so.cache 3848: trying file=/lib64/librt.so.1 3848: 3848: find library=libresolv.so.2 [0]; searching 3848: search cache=/etc/ld.so.cache[root@localhost tools]# ln -s /usr/local/lib/libevent-1.4.so.2 /lib64/
二、php安裝對memcached的支援
php有兩個版本的memcached用戶端
1.memcached
這個是新版的用戶端基於libmemcached,所以必須要安裝libmemcached
先安裝libmemcached
:http://download.tangent.org/libmemcached-0.26.tar.gz
Java代碼
- [root@localhost tools]# tar zxvf libmemcached-0.26.tar.gz
- [root@localhost tools]# cd libmemcached-0.26
- [root@localhost libmemcached-0.26]# ./configure --prefix=/usr/local/libmemcached/ --with-libmemcached-dir=/usr/local/libmemcached/
- [root@localhost libmemcached-0.26]# make
- [root@localhost libmemcached-0.26]# make install
[root@localhost tools]# tar zxvf libmemcached-0.26.tar.gz[root@localhost tools]# cd libmemcached-0.26[root@localhost libmemcached-0.26]# ./configure --prefix=/usr/local/libmemcached/ --with-libmemcached-dir=/usr/local/libmemcached/[root@localhost libmemcached-0.26]# make[root@localhost libmemcached-0.26]# make install
安裝php memcached用戶端
:http://pecl.php.net/get/memcached
Java代碼
- [root@localhost tools]# tar zxvf memcached-0.1.4.tgz
- [root@localhost tools]# cd memcached-0.1.4
- [root@localhost memcached-0.1.4]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/
- [root@localhost memcached-0.1.4]# make
- [root@localhost memcached-0.1.4]# make install
[root@localhost tools]# tar zxvf memcached-0.1.4.tgz[root@localhost tools]# cd memcached-0.1.4[root@localhost memcached-0.1.4]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/[root@localhost memcached-0.1.4]# make[root@localhost memcached-0.1.4]# make install
修改php.ini添加extension = "memcached.so"就可以了。
如果出現錯誤
Java代碼
- checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located
checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located
請先用whereis libmemcached找到路徑,然後添加選項--with-libmemcached-dir=libmemcached路徑
2.memcache
:http://pecl.php.net/get/memcache Java代碼
- [root@localhost tools]# tar zxvf memcache-3.0.3.tgz
- [root@localhost tools]# cd memcache-3.0.3
- [root@localhost tools]# /usr/local/php/bin/phpize
- [root@localhost tools]# ./configure --with-php-config=/usr/local/php/bin/php-config
- [root@localhost tools]# make
- [root@localhost tools]# make install
[root@localhost tools]# tar zxvf memcache-3.0.3.tgz [root@localhost tools]# cd memcache-3.0.3[root@localhost tools]# /usr/local/php/bin/phpize[root@localhost tools]# ./configure --with-php-config=/usr/local/php/bin/php-config[root@localhost tools]# make[root@localhost tools]# make install
修改php.ini添加extension = "memcache.so"就可以了。
提示:如果php找不到so檔案,請設定extension_dir。
ps:這篇東西在草稿箱裡呆很久了,最後還是決定把一篇分多篇發吧,這樣感覺快多了。
參考資料:
http://www.danga.com/memcached/ http://willko.javaeye.com/blog/332993