May 052011
 

Some example accessing memcached from a shell using nc and echo

Storage Commands

set
add
get
gets
replace
append
prepend
cas
delete
command [key] [flags] [exptime] [bytes] [noreply]\r\n[value]\r

Parameters :

[key]			: the key of the data stored
[flags]			: 32-bit unsigned integer that the server store with the data (provided by the user), and return along the data when the item is retrieved
[exptime]		: expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX timestamps for expiration
[bytes]			: number of bytes in the data block
[cas unique]		: unique 64-bit value of an existing entry (retrieved with gets command) to use with cas command
[noreply]		: optional parameter that inform the server to not send the reply


Server return :

STORED		: to indicate success
NOT_STORED	: indicate that the data was not stored because condition for "add" or "replace" command wasn't met, or the item is in a delete queue
EXISTS		: indicate that the item you are trying to store with "cas" command has been modified since last fetch
NOT_FOUND	: indicate that the item did not exist or has been deleted
DELETED		: indicate key has been deleted successfully

$ echo -e 'add my_key 0 60 11\r\nhello world\r' | nc localhost 11211
STORED

$ echo -e 'get my_key\r' | nc localhost 11211
VALUE my_key 0 11
hello world
END

$ echo -e 'replace my_key 0 60 12\r\nhello worlds\r' | nc localhost 11211
STORED

echo -e 'get my_key\r' | nc localhost 11211
VALUE my_key 0 12
hello worlds
END

$ echo -e 'set mykey 0 60 5\r\nhello\r' | nc localhost 11211
STORED

$ echo -e 'get mykey\r' | nc localhost 11211
VALUE mykey 0 5
hello
END

$ echo -e 'delete mykey\r' | nc localhost 11211
DELETED

$ echo -e 'stats\r' | nc localhost 11211
STAT pid 21036
STAT uptime 416508
STAT time 1304564315
.....
.....
STAT curr_items 1
STAT total_items 17
STAT evictions 0
STAT reclaimed 3
END

$ echo -e 'stats items\r' | nc localhost 11211
STAT items:1:number 1
STAT items:1:age 346701
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 3
END

$ echo -e 'stats slabs\r' | nc localhost 11211
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 1
.....
.....
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT active_slabs 1
STAT total_malloced 1048560
END

  3 Responses to “Memcached In A shell Using nc And echo”

Comments (3)
  1. Very helpful! No we got a memached test script that does not need any ruby 🙂

  2. You can also use the option -C for netcat, to ensure CRLF endings and do it interactively:


    $ nc -C localhost 11211
    set name 0 60 5
    value
    STORED
    get name
    VALUE name 0 5
    value
    END

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.