Redis CheatSheet
Introduction
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. This cheat sheet provides a quick reference for common Redis commands and operations.
Command | Description | Example |
---|---|---|
SET key value | Set the value of a key | SET mykey "Hello" |
GET key | Get the value of a key | GET mykey |
DEL key | Delete a key | DEL mykey |
INCR key | Increment the value of a key | INCR counter |
DECR key | Decrement the value of a key | DECR counter |
HSET key field value | Set the value of a hash field | HSET user:id name "John" |
HGET key field | Get the value of a hash field | HGET user:id name |
LPUSH key value | Push a value to the beginning of a list | LPUSH mylist "World" |
RPUSH key value | Push a value to the end of a list | RPUSH mylist "Hello" |
LPOP key | Pop a value from the beginning of a list | LPOP mylist |
RPOP key | Pop a value from the end of a list | RPOP mylist |
SADD key member | Add a member to a set | SADD myset "value" |
SMEMBERS key | Get all members of a set | SMEMBERS myset |
ZADD key score member | Add a member with a score to a sorted set | ZADD myzset 1 "one" |
ZRANGE key start stop | Get a range of members from a sorted set | ZRANGE myzset 0 -1 |
EXPIRE key seconds | Set a key’s time to live in seconds | EXPIRE mykey 60 |
PERSIST key | Remove the expiration of a key | PERSIST mykey |
MSET key1 value1 key2 value2 ... | Set multiple key-value pairs in one command | MSET key1 "value1" key2 "value2" |
GETSET key value | Set the value of a key and return its old value | GETSET mykey "newvalue" |
SCAN cursor [MATCH pattern] [COUNT count] | Incrementally iterate over a set of keys | SCAN 0 MATCH prefix:* COUNT 10 |
INFO | Get information and statistics about the server | INFO |
FLUSHDB | Remove all keys from the current database | FLUSHDB |
BGSAVE | Asynchronously save the dataset to disk | BGSAVE |
BGREWRITEAOF | Asynchronously rewrite the append-only file | BGREWRITEAOF |
CONFIG GET parameter | Get the value of a configuration parameter | CONFIG GET dir |
CONFIG SET parameter value | Set the value of a configuration parameter | CONFIG SET dir /var/redis |
MONITOR | Listen for all requests received by the server | MONITOR |
——————————————– | ————————————————— | ——————————————- |
ZSCORE key member | Get the score associated with a member in a sorted set | ZSCORE myzset "one" |
ZRANK key member | Get the rank of a member in a sorted set | ZRANK myzset "one" |
ZREM key member [member ...] | Remove one or more members from a sorted set | ZREM myzset "one" |
HGETALL key | Get all fields and values from a hash | HGETALL user:id |
HMSET key field1 value1 [field2 value2 ...] | Set multiple hash fields and values | HMSET user:id name "John" age 30 |
HDEL key field1 [field2 ...] | Delete one or more fields from a hash | HDEL user:id age |
SREM key member [member ...] | Remove one or more members from a set | SREM myset "value" |
ZCOUNT key min max | Count the members in a sorted set within a score range | ZCOUNT myzset 0 100 |
ZREVRANGE key start stop [WITHSCORES] | Get a range of members from a sorted set in reverse order | ZREVRANGE myzset 0 -1 WITHSCORES |
LLEN key | Get the length of a list | LLEN mylist |
LINDEX key index | Get an element from a list by its index | LINDEX mylist 2 |
`LINSERT key BEFORE | AFTER pivot value` | Insert a value before or after a specific value in a list |
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] | Get members from a sorted set within a score range | ZRANGEBYSCORE myzset 0 10 WITHSCORES LIMIT 0 5 |
CONFIG REWRITE | Rewrite the configuration file with the in-memory configuration | CONFIG REWRITE |
CLIENT LIST | Get the list of client connections to the server | CLIENT LIST |
SCRIPT LOAD script | Load a Lua script into the script cache | SCRIPT LOAD "return redis.call('PING')" |
This cheat sheet covers some of the fundamental Redis commands. For more detailed information, refer to the official Redis documentation.