memcached是一個高性能的、分布式記憶體對象緩存系統,應用廣泛。 通過緩存資料庫查詢結果,減少資料庫通路次數,以提高動态Web應用的速度、 提高可擴充性。
它可以應對任意多個連接配接,使用非阻塞的網絡IO。由于它的工作機制是在記憶體中開辟一塊空間,然後建立一個HashTable,Memcached自管理這些HashTable。還使用内置的記憶體塊配置設定和哈希表算法,確定虛拟記憶體不會過來搗亂。
二. memcached 的安裝:
注:memcached 用到了libevent這個庫用于Socket的處理,是以還需要安裝libevent.官網:http://www.monkey.org/~provos/libevent/
1. 先安裝libevent:
[root@localhost software]# tar zxvf libevent-1.4.11-stable.tar.gz
[root@localhost libevent-1.4.11-stable]# ./configure –prefix=/usr
[root@localhost libevent-1.4.11-stable]# make
[root@localhost libevent-1.4.11-stable]# make install
2. 測試libevent是否安裝成功
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 07-10 13:10 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x 1 root root 31596 2007-01-07 libevent-1.1a.so.1.0.2
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root root 308088 07-21 03:33 libevent-1.4.so.2.1.3
-rw-r--r-- 1 root root 394474 07-21 03:33 libevent.a
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root root 109490 07-21 03:33 libevent_core-1.4.so.2.1.3
-rw-r--r-- 1 root root 148742 07-21 03:33 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 246870 07-21 03:33 libevent_extra-1.4.so.2.1.3
-rw-r--r-- 1 root root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
安裝OK。
3. 安裝memcached,同時需要安裝中指定libevent的安裝位置
[root@localhost software]# tar zxvf memcached-1.4.0.tar.gz
[root@localhost memcached-1.4.0]# ./configure –with-libevent=/usr
[root@localhost memcached-1.4.0]# make
[root@localhost memcached-1.4.0]# make intall
4. 測試是否成功安裝memcached
[root@localhost memcached-1.4.0]# ls -al /usr/local/bin | grep memcached
-rwxr-xr-x 1 root root 188225 07-21 03:35 memcached
三. 如何啟動 memcached 服務:
隻需要啟動一個 memcached 監護程序,監護程序不需要配置檔案,隻要在指令行裡面加三四個參數就可以了:
[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
-d: (run as a daemon) 選項是啟動一個守護程序
-m:(max memory to use for items in megabytes (default: 64 MB))是配置設定給Memcache使用的記憶體數量,機關是MB,我這裡是100MB,
-u:(assume identity of <username> (only when run as root))是運作Memcache的使用者,我這裡是root,
-l:(interface to listen on)是監聽的伺服器IP位址,如果有多個位址的話,這裡指定了伺服器的IP位址127.0.0.1,
-p:是設定Memcache監聽的端口,這裡設定了11211,最好是1024以上的端口,
-c:選項是最大運作的并發連接配接數,預設是1024,這裡設定了256,根據伺服器的負載量來設定,
-P:(save PID in <file>, only used with -d option)是設定儲存Memcache的pid檔案,這裡是儲存在 /tmp/memcached.pid
注:也可以啟動多個守護程序,不過端口不能重複。
四. 安裝 Memcached 的PHP擴充:
在PHP中使用Memcached,有兩種方式:
一種是安裝PHP的memcached擴充。該擴充是用c寫的,效率較高,需要在伺服器上安裝。
另外一種則是直接使用用戶端的php-memcached-client類庫。
下面是使用PECL中Memcache的專用擴充,因為畢竟是用C寫的,效率高,而且安裝部署起來也比較友善。
1. 在 http://pecl.php.net/package/memcache 選擇相應想要下載下傳的memcache版本。我下載下傳的是:memcache-2.2.5.tgz 版本。
2. 安裝 memcache
[root@localhost software]# tar zxvf memcache-2.2.5.tgz
[root@localhost software]# cd memcache-2.2.5
[root@localhost memcache-2.2.5]# /usr/bin/phpize
[root@localhost memcache-2.2.5]# ./configure –enable-memcache –with-php-config=/usr/bin/php-config –with-zlib-dir
[root@localhost memcache-2.2.5]# make
[root@localhost memcache-2.2.5]# make install
這步會有類似這樣的提示:Installing shared extensions: /usr/local/php/modules
3. 把/etc/php.ini中的extension_dir = “./”修改為:extension_dir = "/usr/lib/php/modules"
4. 并添加: extension=memcache.so
也可執行以下shell指令,對php.ini檔案的修改:
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/n#' /usr/local/webserver/php/etc/php.ini
五. 安裝C/C++ Memcached用戶端庫:libmemcached
下載下傳:http://download.tangent.org/libmemcached-0.32.tar.gz
1. 安裝 libmemcached
[root@localhost src]# tar zxvf libmemcached-0.32.tar.gz
[root@localhost src]# cd libmemcached-0.32
[root@localhost libmemcached-0.32]# ./configure --prefix=/usr
[root@localhost libmemcached-0.32]# make && make install
2. 檢查安裝結果
[root@localhost src]# ls /usr/lib/libmemcache* //庫檔案
[root@localhost src]# ls /usr/include/libmemcached/* //頭檔案
[root@localhost src]# ls /usr/bin/mem* //指令行工具
六. 應用:
1. 啟動 memcache 服務
2. 重新開機 Web 伺服器
[root@localhost bin]# service httpd restart
一、Memcache面向對象的常用接口包括:
Memcache::connect — 打開一個到Memcache的連接配接
Memcache::pconnect — 打開一個到Memcache的長連接配接
Memcache::close — 關閉一個Memcache的連接配接
Memcache::set — 儲存資料到Memcache伺服器上
Memcache::get — 提取一個儲存在Memcache伺服器上的資料
Memcache::replace — 替換一個已經存在Memcache伺服器上的項目
Memcache::delete — 從Memcache伺服器上删除一個儲存的項目
Memcache::flush — 重新整理所有Memcache伺服器上儲存的項目(類似于删除所有的儲存的項目)
Memcache::getStats — 擷取目前Memcache伺服器運作的狀态
二、檢視系統的運作狀态:
pid Process id of this server process (memcache伺服器的程序ID)
uptime Number of seconds this server has been running (伺服器已經運作的秒數)
time Current UNIX time according to the server (伺服器目前的UNIX時間)
version Version string of this server (memcache版本)
pointer_size Current system pointer 目前作業系統的指針大小(32位系統一般是32bit)
rusage_user Accumulated user time for this process (該程序累計的使用者時間(秒:微妙))
rusage_system Accumulated system time for this process (該程序累計的系統時間(秒:微妙))
curr_items Current number of items stored by the server (伺服器目前存儲的内容數量)
total_items Total number of items stored by this server ever since it started (伺服器啟動以來存儲過的内容總數)
bytes Current number of bytes used by this server to store items (伺服器目前存儲内容所占用的位元組數)
curr_connections Number of open connections (目前打開着的連接配接數量)
total_connections Total number of connections opened since the server started running (伺服器運作以來接受的連接配接總數)
connection_structures Number of connection structures allocated by the server (伺服器配置設定的連接配接結構的數量)
cmd_get Cumulative number of retrieval requests (get指令(擷取)總請求次數)
cmd_set Cumulative number of storage requests (set指令(儲存)總請求次數)
get_hits Number of keys that have been requested and found present (請求成功的總次數)
get_misses Number of items that have been requested and not found (請求失敗的總次數)
threads Current number of thread (目前線程數)
bytes_read Total number of bytes read by this server from network (伺服器從網絡讀取到的總位元組數)
bytes_written Total number of bytes sent by this server to network (伺服器向網絡發送的總位元組數)
limit_maxbytes Number of bytes this server is allowed to use for storage. (伺服器在存儲時被允許使用的位元組總數)
evictions Number of valid items removed from cache to free memory for new items (為擷取空閑記憶體而删除的items數(配置設定給memcache的空間用滿後需要删除舊的items來得到空間配置設定給新的items))
其中,最關注最多的幾個參數:
uptime:是memcached運作的秒數。
cmd_get:是查詢緩存的次數。
cmd_get/uptime 結果是平均每秒請求緩存的次數——結果值越大,說明Memcached的使用率越高,站點的通路量大,如果太低,用檔案系統緩存就可以了,根本不會展現出使用memcached的強大性能。
cmd_set:是設定key=>value的次數。整個memcached是個大hash,用cmd_get沒有找到的内容,就會調用一下cmd_set寫進緩存裡。
get_hits:是緩存命中的次數。所謂的命中率 = get_hits/cmd_get * 100%。
get_misses:是緩存未命中的次數。get_misses加上get_hits就等于cmd_get。
stats:顯示伺服器資訊、統計資料等
stats reset:清空統計資料
stats slabs:顯示各個slab的資訊,包括chunk的大小、數目、使用情況等
stats items:顯示各個slab中item的數目和存儲時長(最後一次通路距離現在的秒數)
quit:退出
三、利用shell指令操作Memcached
1、資料存儲(key為wan,value為123)
2、資料取回
3、替換資料(将以wan為key存儲的值替換為122)
4、數值增加 1
5、數值減少 2
6、資料删除
7、檢視Memcached當時狀态
printf “stats\r\n” | nc 127.0.0.1 11211
8、檢視Memcached實時狀态
watch “printf ‘stats\r\n’ | nc 127.0.0.1 11211″
Memcached protocol 中英文檔可以參考:
<a href="http://blog.s135.com/book/memcached/" target="_blank">http://blog.s135.com/book/memcached/</a>
四. 檢視slabs的使用狀況
使用memcached的創造着Brad寫的名為 memcached-tool 的Perl腳本,可以友善地獲得slab的使用情況(它将memcached的傳回值整理成容易閱讀的格式)。可以從下面的位址獲得腳本:
<a href="http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool" target="_blank">http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool</a>
[root@localhost html]# vim memcached-tool
[root@localhost html]# chmod +x memcached-tool
[root@localhost html]# ./memcached-tool 127.0.0.1:11211
# Item_Size Max_age 1MB_pages Count Full?
1 80 B 0 s 1 0 no
2 104 B 12175 s 1 1 no
3 176 B 1339587 s 33 196567 yes
各列的含義:
#: slab class編号
Item_Size: Chunk大小
Max_age: LRU内最舊的記錄的生存時間
1MB_pages: 配置設定給Slab的頁數
Count: Slab内的記錄數
Full?: Slab内是否含有空閑chunk
五. 也可以圖形化監控 Memcached 的運作狀态
<a href="http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/" target="_blank">http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/</a>
是一個PHP源檔案,隻需要修改源碼中的使用者名、密碼以及數組$MEMCACHE_SERVERS 就可以了。
存儲指令的格式:
<code><command name> <key> <flags> <exptime> <bytes></code>
<code><data block></code>
參數說明如下:
<command name>
set/add/replace
<key>
查找關鍵字
<flags>
客戶機使用它存儲關于鍵值對的額外資訊
<exptime>
該資料的存活時間,0表示永遠
<bytes>
存儲位元組數
<data block>
存儲的資料塊(可直接了解為key-value結構中的value)
(1)、無論如何都存儲的set
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815074223.jpg" target="_blank"></a>
這個set的指令在memcached中的使用頻率極高。set指令不但可以簡單添加,如果set的key已經存在,該指令可以更新該key所對應的原來的資料,也就是實作更新的作用。
可以通過“get 鍵名”的方式檢視添加進去的記錄:
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815075585.jpg" target="_blank"></a>
如你所知,我們也可以通過delete指令删除掉,然後重新添加。
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/20111106181507535.jpg" target="_blank"></a>
(2)、隻有資料不存在時進行添加的add
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815087122.jpg" target="_blank"></a>
(3)、隻有資料存在時進行替換的replace
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815085976.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815096467.jpg" target="_blank"></a>
可以看到,删除已存在的鍵值和不存在的記錄可以傳回不同的結果。
get指令的key可以表示一個或者多個鍵,鍵之間以空格隔開
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815098909.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815103303.jpg" target="_blank"></a>
可以看到,gets指令比普通的get指令多傳回了一個數字(上圖中為13)。這個數字可以檢查資料是否發生改變。當key對應的資料改變時,這個多傳回的數字也會改變。
cas即checked and set的意思,隻有當最後一個參數和gets所擷取的參數比對時才能存儲,否則傳回“EXISTS”。
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815113793.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815118187.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815124774.jpg" target="_blank"></a>
執行stats items,可以看到STAT items行,如果memcached存儲内容很多,那麼這裡也會列出很多的STAT items行。
我們執行stats cachedump 1 0 指令效果如下:
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815126136.jpg" target="_blank"></a>
這裡slab_id為1,是由2中的stats items傳回的結果(STAT items後面的數字)決定的;limit_num看起來好像是傳回多少條記錄,猜的一點不錯, 不過0表示顯示出所有記錄,而n(n>0)就表示顯示n條記錄,如果n超過該slab下的所有記錄,則結果和0傳回的結果一緻。
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815138579.jpg" target="_blank"></a>
通過stats items、stats cachedump slab_id limit_num配合get指令可以周遊memcached的記錄。
如stats slabs,stats sizes,stats reset等等使用也比較常見。
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815132973.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815149875.jpg" target="_blank"></a>
在現有的緩存資料後添加緩存資料,如現有緩存的key不存在伺服器響應為NOT_STORED。
和append非常類似,但它的作用是在現有的緩存資料前添加緩存資料。
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061815141445.jpg" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/jeffwongishandsome/201111/201111061809106009.jpg" target="_blank"></a>
該指令有一個可選的數字參數。它總是執行成功,伺服器會發送 “OK\r\n” 回應。它的效果是使已經存在的項目立即失效(預設),或在指定的時間後。此後執行取回指令,将不會有任何内容傳回(除非重新存儲同樣的鍵名)。 flush_all 實際上沒有立即釋放項目所占用的記憶體,而是在随後陸續有新的項目被儲存時執行(這是由memcached的懶惰檢測和删除機制決定的)。
flush_all 效果是它導緻所有更新時間早于 flush_all 所設定時間的項目,在被執行取回指令時指令被忽略。
memcached還有很多指令,比如對于存儲為數字型的可以通過incr/decr指令進行增減操作等等,這裡隻列出開發和運維中經常使用的指令,其他的不再一一舉例說明。
本文轉自 chengxuyonghu 51CTO部落格,原文連結:http://blog.51cto.com/6226001001/1727469,如需轉載請自行聯系原作者