Memcache
在許多web應用中都將數據保存到RDBMS中,應用服務器從中讀取出來並在瀏覽器上顯示。但是隨着數據量的增大、訪問的集中,就會出現RDBMS的負擔加重、數據庫相應惡化、網站顯示延遲等重大影響。
這時就該memcached大顯身手了。Memcached是提高性能的分佈式內存緩存服務器。一般的使用目的是,通過緩存數據庫查詢的結果,減少數據庫訪問的次數,以提高動態web應用的速度、提高可擴展性。
Memcached的特徵
協議簡單
基於libevent的時間處理
內置內存存儲方式
Memcached不互相通信的分佈式
使用memcached提高性能
memcached的參數
-p監聽端口
-l<ip_addr>連接的ip地址默認是本機的
Listenon<ip_addr>;defaulttoINADDR_ANY.Thisisanimportant
-dstart啓動memcached作爲一個服務Runmemcachedasadaemon.
Restart重啓
Stop|shutdown關閉服務
Install安裝服務
Uninstall卸載
-u<username>以什麼身份運行
Assumetheidentityof<username>(onlywhenrunasroot).
-m<num>最大的內存使用
Use<num>MBmemorymaxtous
-f<factor>塊大小增長因子,默認是1.25
Use<factor>asthemultiplierforcomputingthesizesofmemory
-M內存消耗盡時返回的錯誤,而不是刪除項
chunksthatitemsarestoredin.Alowervaluemayresultin
lesswastedmemorydependingonthetotalamountofmemory
availableandthedistributionofitemsizes.Thedefaultis
1.25.
-n<size>最小分配空間
lamp+memcached--->lammp
1.安裝libevent
2.memcached服務器,memcached基於libevent的事件處理機制,所以要指明libevent的安裝路徑
./configure--with-libevent=/usr/local/libevent/
make&&makeinstall
開啓以nobody的身份memcached服務
memcached-unobody-dstart
查看memcached的進程及監聽端口
手工編寫memcached服務控制腳本
#chkconfig:23458999
#description:httpdservice
lockfile=/var/lock/subsys/memcached
prog=/usr/local/memcache/bin/memcached
./etc/init.d/functions
start(){
[-f$lockfile]&&echo"memcachedisstarted"&&exit
echo-n"memcachedisstarting....."
sleep1
$prog-unobody-dstart&&echo"ok"&&touch$lockfile||echo"failed"
}
stop(){
[!-f$lockfile]&&echo"memcachedisstoped"&&exit
echo-n"memcachedisstoping....."
sleep1
killprocmemcached&&echo"ok"&&rm-rf$lockfile||echo"failed"
}
case"$1"in
start)
Start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo"usage:(start|stop|restart)"
;;
Esac
重啓memcached服務
Servicememcachedrestart
[root@localhostbin]#netstat-tupln|grepmemcached
tcp000.0.0.0:112110.0.0.0:*LISTEN16174/memcached
tcp00:::11211:::*LISTEN16174/memcached
udp000.0.0.0:112110.0.0.0:*16174/memcached
udp00:::11211:::*16174/memcached
把memcached控制腳本加入到chkconfig,在345級別下開機自動啓動memcached服務
Chkconfig--addmemcached
Chkconfig--list|grepmemcached
[root@localhostbin]#chkconfig--list|grepmemcached
memcached0:off1:off2:on3:on4:on5:on6:off
Telnet192.168.20.20011211
Stats查看memcached的狀態
STATpid19681memcached的進程id
STATuptime1858
STATtime1378208617服務開啓的時間
STATversion1.4.13memcached的版本
STATlibevent2.0.16-stable
STATpointer_size32
STATrusage_user0.002999
STATrusage_system0.005999
STATcurr_connections10
STATtotal_connections11
STATconnection_structures11
STATreserved_fds20
STATcmd_get0
STATcmd_set0
STATcmd_flush0
STATcmd_touch0
STATget_hits0get下載鍵值的內容
STATget_misses0
STATdelete_misses0
STATdelete_hits0刪除鍵值
STATincr_misses0
STATincr_hits0
STATdecr_misses0
STATdecr_hits0
STATcas_misses0
STATcas_hits0
STATcas_badval0
STATtouch_hits0
STATtouch_misses0
STATauth_cmds0
STATauth_errors0
STATbytes_read15
STATbytes_written14
STATlimit_maxbytes67108864
STATaccepting_conns1
STATlisten_disabled_num0
STATthreads4
STATconn_yields0
STAThash_power_level16
STAThash_bytes262144
STAThash_is_expanding0
STATexpired_unfetched0
STATevicted_unfetched0
STATbytes0
STATcurr_items0
STATtotal_items0
STATevictions0
STATreclaimed0
END
addnewkey0605add添加鍵值
12345鍵值的內容
STORED
getnewkey下載鍵值
VALUEnewkey05
12345
END
3.爲php添加memchace擴展,安裝php的memcache客戶端,指明php的配置文件所在的路徑
執行/usr/local/php/bin/phpize產生memcache的安裝配置文件
./configure--enable-cache--with-php-config=/usr/local/php/bin/php-config
在/usr/local/php/lib/php/extensions/no-debug-zts-20090626/目錄下形成memcache的模塊
打開php的初始化文件/etc/php.ini,添加一條語句指明memcache模塊所在的位置
Extension=/usr/local/php/lib/php/extensions/no-debug-zts-20090626/