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:
Command | Description |
---|---|
memcached | Start Memcached server |
memcached -d -p 11211 | Start Memcached daemon on port 11211 |
telnet localhost 11211 | Connect to Memcached server |
set key 0 60 5 | Store a value with key, flags, expiration, and size |
add key 0 60 5 | Add a value only if the key does not exist |
replace key 0 60 8 | Replace a value only if the key exists |
append key 0 60 3 | Append data to an existing value |
prepend key 0 60 4 | Prepend data to an existing value |
cas key 0 60 9 | Compare and swap - update if the value hasn’t changed |
get key | Retrieve the value associated with the key |
gets key | Retrieve the value and its unique CAS identifier |
delete key | Remove the key and its value |
incr key 2 | Increment the value of the key by 2 |
decr key 1 | Decrement the value of the key by 1 |
flush_all | Flush all items from the cache |
stats | Display server statistics |
version | Display Memcached version |
quit | Close the connection to the Memcached server |
stats reset | Reset server statistics |
stats items | Display item-related statistics |
stats slabs | Display slab-related statistics |
stats sizes | Display sizes-related statistics |
stats detail on | Enable verbose mode for statistics |
stats detail off | Disable verbose mode for statistics |
touch key 120 | Update the expiration time for the key |
flush_all 5 | Flush all items after a delay of 5 seconds |
verbosity 2 | Set server verbosity level |
lru_crawler metadump 1 | Request a dump of the metadata |
lru_crawler enable | Enable the LRU crawler background job |
lru_crawler disable | Disable the LRU crawler background job |
config get maxbytes | Retrieve the configured maximum memory size |
config set maxbytes 67108864 | Set the maximum memory size to 64MB |
slabs reassign 0 1 | Move items from slab class 0 to slab class 1 |
watch key | Watch the key for modifications |
stats cachedump 0 0 | Dump all items from slab class 0 |
stats cachedump 1 0 | Dump all items from slab class 1 |
stats cachedump 2 100 | Dump up to 100 items from slab class 2 |
config get slab_automove | Retrieve slab automove settings |
config set slab_automove 1 | Enable slab automove |
config set slab_automove 0 | Disable slab automove |
config get tail_repair_time | Retrieve tail-fragmentation avoidance settings |
config set tail_repair_time 30 | Set tail-fragmentation avoidance time to 30 seconds |
version | Display 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.