天天看點

memcached 操作

參見文章

1.通路memcached

telnet 127.0.0.1 11211
           

2.顯示狀态

stats
           
pid = process id
uptime = number of seconds since the process was started
time = current time
version = memcached version
rusage_user = seconds the cpu has devoted to the process as the user
rusage_system = seconds the cpu has devoted to the process as the system
curr_items = total number of items currently in memcache
total_items = total number of items that have passed through the cache
bytes = total number of bytes currently in use by curr_items
curr_connections = total number of open connections to memcached
connection_structures = ???
cmd_get = total GET commands issued to the server
cmd_set = total SET commands issued to the server
get_hits = total number of times a GET command was able to retrieve and
return data
get_misses = total number of times a GET command was unable to retrieve and
return data
bytes_read = total number of bytes input into the server
bytes_written = total number of bytes written by the server
limit_maxbytes = total storage bytes available to the server.
           

    limit_maxbytes、bytes

    memcached在存儲的時候是可以設定失效時間的,但如果存儲已經滿了,那舊資料即使沒有到過期時間,也會被移除。是以需要觀察memcached存儲是否已經滿了,同時這對擴容也是有意義的參考。limit_maxbytes即總的存儲大小,而bytes就是已經使用的大小,從這兩個資料就可以看出在memcached啟動時,我們為它配置設定的記憶體是否足夠使用。

    cmd_get、cmd_set

    memcached啟動後,我們對它一共做了多少次讀取操作呢?從這兩個參數可以觀察出來。

    get_hits、get_misses

    使用memcached後,我們需要評估我們使用的政策是否合理。不能夠使用中間緩存後,後端的資料庫還是有較大的通路量,這樣的話中間緩存就變得沒有意義了。get_hits表示命中了多少次讀取,即來memcached取到了多少有效資料;get_misses表示沒有命中的次數,即此次來取資料的時候,memcached并沒有你所查詢的資料。如果沒有清零統計資料的話,cmd_get = get_hits + get_misses。

3. 清空統計資料

stats reset
           

4.顯示某個slab中的前limit_num個key清單

stats cachedump 1 2
           

5. 顯示slabs資訊

stats slabs
           

6.指令說明

<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
a) <command name> 可以是”set”, “add”, “replace”。
“set”表示按照相應的<key>存儲該資料,沒有的時候增加,有的覆寫。
“add”表示按照相應的<key>添加該資料,但是如果該<key>已經存在則會操作失敗。
“replace”表示按照相應的<key>替換資料,但是如果該<key>不存在則操作失敗

b) <key> 用戶端需要儲存資料的key。

c) <flags> 是一個16位的無符号的整數(以十進制的方式表示)。
該标志将和需要存儲的資料一起存儲,并在用戶端get資料時傳回。
客戶可以将此标志用做特殊用途,此标志對伺服器來說是不透明的。

d) <exptime> 過期的時間。
若為0表示存儲的資料永遠不過時(但可被伺服器算法:LRU 等替換)。
如果非0(unix時間或者距離此時的秒數),當過期後,伺服器可以保證使用者得不到該資料(以伺服器時間為标準)。

e) <bytes> 需要存儲的位元組數(不包含最後的”\r\n”),當使用者希望存儲空資料時,<bytes>可以為0

f) 最後用戶端需要加上”\r\n”作為”指令頭”的結束标志。
<data block>\r\n

緊接着”指令頭”結束之後就要發送資料塊(即希望存儲的資料内容),最後加上”\r\n”作為此次通訊的結束。

結果響應:reply
當以上資料發送結束之後,伺服器将傳回一個應答。
           

7.添加一個新的條目到memcached或是用新的資料替換掉已存在的條目

set key 0 0 value長度
value

set test1 0 0 10
testing001
           

8.僅當key不存在的情況下存儲資料。如果一個key已經存在,将得到NOT_STORED的響應

add key 0 0 value長度
value

add test1 0 0 10
testing002
           

9.僅當key已經存在的情況下存儲資料。如果一個key不存在,将得到NOT_STORED的響應

replace key 0 0 value長度
value

replace test1 0 0 10
testing002
           

10.從memcached中傳回資料。從緩存中傳回資料時,将在第一行得到key的名字,flag的值和傳回的value的長度。真正的資料在第二行,最後傳回END

get key

get testing1
           

11.啟動Memcache 常用參數

-p <num>      設定端口号(預設不設定為: 11211)
-U <num>      UDP監聽端口 (預設: 11211, 0 時關閉)  
-l <ip_addr>  綁定位址 (預設:所有都允許,無論内外網或者本機更換IP,有安全隐患,若設定為127.0.0.1就隻能本機通路)
-d            獨立程序運作
-u <username> 綁定使用指定用于運作程序 <username>
-m <num>      允許最大記憶體用量,機關M (預設: 64 MB)
-P <file>     将PID寫入檔案<file>,這樣可以使得後邊進行快速程序終止, 需要與 -d 一起使用
如:
在linux下:./usr/local/bin/memcached -d -u jb-mc -l 192.168.1.197 -m 2048 -p 12121
在window下:d:\memcached\memcached.exe -d RunService -l 127.0.0.1 -p 11211 -m 500
在windows下注冊為服務後運作:
sc.exe create my-Memcached binpath="d:\memcached\memcached.exe -d RunService -p 11211 -m 500" start=auto
net start my-Memcached
           

12.顯示所有條目

stats items
           

13.删除條目

delete <key> <time>

a) <key> 需要被删除資料的key
b) <time> 用戶端希望伺服器将該資料删除的時間(unix時間或者從現在開始的秒數)
           

14. 清空所有鍵值

flush_all
           

15.退出

quit