Catalog / Memcached Cheatsheet
Memcached Cheatsheet
A quick reference for Memcached, covering basic commands, data management, and common use cases. Ideal for developers needing a fast, distributed memory object caching system.
Core Concepts & Commands
Basic Operations
|
Stores data under the specified key. Flags are arbitrary 16-bit integer, exptime is expiration time in seconds (0 means never expire), bytes is data length. Example: |
|
Retrieves data associated with the specified key. Example: |
|
Stores data under the specified key, but only if the server doesn’t already hold data for this key. Example: |
|
Stores data under the specified key, but only if the server does already hold data for this key. Example: |
|
Deletes data associated with the specified key. Time is an optional delay before deletion (in seconds). Example: |
|
Increments the value of the key by value. The value must be an integer. Example: |
|
Decrements the value of the key by value. The value must be an integer. Example: |
Flags
Flags are an arbitrary 16-bit integer that the client stores along with the data. It is opaque to the server. |
Advanced Features
CAS (Check and Set)
|
Retrieves data with a unique CAS identifier. Example: |
|
Stores data only if the CAS identifier matches the current value. Prevents race conditions. Example: |
Multi-Get
Retrieves multiple keys in a single request, reducing network overhead. Example: |
Expiration
Expiration time is specified in seconds. A value of 0 means the item never expires (although it may be evicted from the cache if memory is needed). If the expiration time is greater than 30 days (2592000 seconds), it is treated as a Unix timestamp. |
LRU (Least Recently Used)
Memcached uses an LRU algorithm to evict items from the cache when it runs out of memory. Least recently used items are removed first. |
Configuration & Management
Starting Memcached
Starts Memcached with specified memory allocation, port, user, and as a daemon. Example: |
Configuration Options
|
Sets the maximum memory to use for cache items, in MB. |
|
Sets the port number to listen on (default: 11211). |
|
Runs the daemon as the specified user. |
|
Runs Memcached as a daemon. |
|
Bind Memcached to a specific IP address. Useful for multi-homed servers. |
|
Sets the maximum number of concurrent connections. |
Stats
Displays various statistics about the Memcached server, such as uptime, cache hits, misses, and memory usage. Example Output: |
Client Libraries
Popular Libraries
PHP |
|
Python |
|
Java |
|
Ruby |
|
Node.js |
|
Basic Usage (Python - pymemcache)
|