This tutorial shows us how to install and interact with memcached server in terminal. For more info, check cheat sheet and use phpMemcachedAdmin browser GUI.


Install Memcached


sudo apt-get install php5 php5-dev php5-memcached memcached
sudo apt-get install memdump

Verify installation


echo "stats settings" | nc localhost 11211

Check what IP and port memcached is running


ps -ef

Live run-time stats


watch "echo stats | nc 127.0.0.1 11211"

Login


telnet 127.0.0.1 11211

List all items


Use stats items to list all items and dump details of given item with stats cachedump 3 100.


telnet 127.0.0.1 11211

stats items
STAT items:3:number 1
STAT items:3:age 498
STAT items:22:number 1
STAT items:22:age 498
END

# The first number after ‘items’ is the slab id. Request a cache dump for each slab id, with a limit for the max number of keys to dump

stats cachedump 3 100
ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s]
END

stats cachedump 22 100
ITEM views.decorators.cache.cache_page..8427e [7736 b; 1256056128 s]
END

Setting and getting keys


set my_key_name 0 86400 7 # Set with a "flag" of 0, a TTL of 86400 seconds and 7 bytes of data
inanzzz # Value to set is entered on a separate line. Length matches the number 7 above

get my_key_name # Get the key "my_key_name" from memcached and return "inanzzz"