Memcached Cheat Sheet

Memcached is a high-performance, distributed memory object caching system used to speed up dynamic web applications by reducing database load. This cheatsheet serves as a quick reference for common Memcached commands and configurations.

Commands and Descriptions:

CommandDescription
memcachedStart Memcached server
memcached -d -p 11211Start Memcached daemon on port 11211
telnet localhost 11211Connect to Memcached server
set key 0 60 5Store a value with key, flags, expiration, and size
add key 0 60 5Add a value only if the key does not exist
replace key 0 60 8Replace a value only if the key exists
append key 0 60 3Append data to an existing value
prepend key 0 60 4Prepend data to an existing value
cas key 0 60 9Compare and swap - update if the value hasn’t changed
get keyRetrieve the value associated with the key
gets keyRetrieve the value and its unique CAS identifier
delete keyRemove the key and its value
incr key 2Increment the value of the key by 2
decr key 1Decrement the value of the key by 1
flush_allFlush all items from the cache
statsDisplay server statistics
versionDisplay Memcached version
quitClose the connection to the Memcached server
stats resetReset server statistics
stats itemsDisplay item-related statistics
stats slabsDisplay slab-related statistics
stats sizesDisplay sizes-related statistics
stats detail onEnable verbose mode for statistics
stats detail offDisable verbose mode for statistics
touch key 120Update the expiration time for the key
flush_all 5Flush all items after a delay of 5 seconds
verbosity 2Set server verbosity level
lru_crawler metadump 1Request a dump of the metadata
lru_crawler enableEnable the LRU crawler background job
lru_crawler disableDisable the LRU crawler background job
config get maxbytesRetrieve the configured maximum memory size
config set maxbytes 67108864Set the maximum memory size to 64MB
slabs reassign 0 1Move items from slab class 0 to slab class 1
watch keyWatch the key for modifications
stats cachedump 0 0Dump all items from slab class 0
stats cachedump 1 0Dump all items from slab class 1
stats cachedump 2 100Dump up to 100 items from slab class 2
config get slab_automoveRetrieve slab automove settings
config set slab_automove 1Enable slab automove
config set slab_automove 0Disable slab automove
config get tail_repair_timeRetrieve tail-fragmentation avoidance settings
config set tail_repair_time 30Set tail-fragmentation avoidance time to 30 seconds
versionDisplay Memcached version

Note: Adjust the parameters (e.g., key, flags, expiration) based on your specific use case. Always refer to the official Memcached documentation for detailed information.