Monday 26 May 2014

memcached client commands

Memcached is a very powerful caching tool and widely used in Internet-based company. We usually consider it as a key-value NoSQL. In this blog, I will introduce some of the common Memcached client commands.


1. connect to memcached

memcached even does not provide any client tool, we can use telnet client to connect to the Memcached database.

[root@X001 ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.

2. set

set command is used to set key-value pair to the memcached


<command name> <key> <flags> <exptime> <bytes>
<data block>

parameters:


<command >set
<key>key
<flags>integer, used by client as additional field
<exptime>keep alive time (seconds), use 0 for never expired
<bytes>bytes 
<data block>value

for example:
set a 999 0 2
10
STORED

3. add and replace

Set will replace the previous value if the key exist, if you don't want the old value to be overwritten, you can use 'add' instead. if the key is there, it will give out a 'not_stored' warning message.
add a 999 0 2
11
NOT_STORED

If you just wants to update the value, you can use 'replace' to set the value, if the key is not in memcached, it will give out a 'not_stored' warning message.

replace b 99 0 2
44
STORED

4. get and gets

get will return the value of a key while gets will add a version. both of them support get multiple values at a time.

for example:

get b
VALUE b 88 2
tt
END
gets b
VALUE b 88 2 7 # version number
tt
END
gets a b #get a and b
VALUE a 888 2 4
12
VALUE b 88 2 7
tt
END

5. delete and flush_all

delete command will delete the value by key. flush_all will just flush the all key-value data.

delete a noreply
get a
END
delete b
DELETED

6. STATS

stats command give the admin the running status for the memcached process
stats
STAT pid 1729
STAT uptime 5865
STAT time 1401159261
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 1.018845
STAT rusage_system 1.179820
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 18
STAT cmd_set 14
STAT cmd_flush 2
STAT get_hits 13
STAT get_misses 5
STAT delete_misses 0
STAT delete_hits 2
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 503
STAT bytes_written 1432
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 9
STAT evictions 0





No comments:

Post a Comment