Introduction to Redis Commands
Redis commands are simple, yet powerful, allowing users to interact with the Redis database effectively. Each command consists of a command name followed optionally by one or more arguments. The syntax is straightforward, which contributes to Redis's ease of use and rapid adoption. The commands can be categorized into several types based on their purpose:
- Key commands
- Value commands
- List commands
- Set commands
- Hash commands
- Sorted Set commands
- Pub/Sub commands
- Transactions
- Scripting
- Server commands
This cheat sheet will provide a concise overview of these categories, listing essential commands along with brief descriptions.
Key Commands
Key commands are fundamental for managing the keys in a Redis database. Here are some of the most commonly used key commands:
Basic Key Operations
1. DEL key: Deletes a key.
2. EXISTS key: Checks if a key exists (returns 1 if it does, 0 if it doesn’t).
3. EXPIRE key seconds: Sets a time-to-live (TTL) on a key in seconds.
4. TTL key: Returns the remaining time-to-live of a key.
5. RENAME old_key new_key: Renames a key.
6. DUMP key: Serializes the value stored at the specified key for persistence.
7. TYPE key: Returns the type of value stored at a key (string, list, set, etc.).
Key Pattern Matching
- KEYS pattern: Returns all keys matching the specified pattern. (Use with caution in production as it can block the server.)
- SCAN cursor: Iterates through keys in the database, returning a cursor for pagination.
Value Commands
Value commands allow you to manipulate simple string values in Redis.
String Operations
1. SET key value: Sets the value of a key.
2. GET key: Retrieves the value of a key.
3. MSET key1 value1 key2 value2 ...: Sets multiple keys at once.
4. MGET key1 key2 ...: Retrieves multiple keys at once.
5. INCR key: Increments the integer value of a key by one.
6. DECR key: Decrements the integer value of a key by one.
7. APPEND key value: Appends a value to the existing value of a key.
String Manipulation
- STRLEN key: Returns the length of the string value stored at a key.
- GETSET key value: Sets the value of a key and returns the old value.
List Commands
Redis supports lists, which are ordered collections of strings.
List Operations
1. LPUSH key value: Inserts a value at the head of the list.
2. RPUSH key value: Inserts a value at the tail of the list.
3. LPOP key: Removes and returns the first element of the list.
4. RPOP key: Removes and returns the last element of the list.
5. LRANGE key start stop: Returns a range of elements from the list.
6. LLEN key: Returns the length of the list.
7. LREM key count value: Removes the first count occurrences of the value from the list.
Set Commands
Sets in Redis are unordered collections of unique strings.
Set Operations
1. SADD key member: Adds a member to the set.
2. SREM key member: Removes a member from the set.
3. SMEMBERS key: Returns all members of the set.
4. SCARD key: Returns the number of members in the set.
5. SISMEMBER key member: Checks if a member is in the set.
6. SUNION key1 key2 ...: Returns the union of multiple sets.
7. SINTER key1 key2 ...: Returns the intersection of multiple sets.
8. SDIFF key1 key2 ...: Returns the difference between sets.
Hash Commands
Hashes are maps between string field and string values, allowing you to represent objects.
Hash Operations
1. HSET key field value: Sets the value of a field in a hash.
2. HGET key field: Gets the value of a field in a hash.
3. HDEL key field: Deletes a field from a hash.
4. HGETALL key: Gets all fields and values in a hash.
5. HKEYS key: Returns all field names in a hash.
6. HVALS key: Returns all values in a hash.
7. HINCRBY key field increment: Increments the integer value of a field by a specified amount.
Sorted Set Commands
Sorted sets are similar to sets but with an associated score for each member, enabling ordered retrieval.
Sorted Set Operations
1. ZADD key score member: Adds a member with a score to the sorted set.
2. ZREM key member: Removes a member from the sorted set.
3. ZRANGE key start stop: Returns members in a specified range by index.
4. ZRANGEBYSCORE key min max: Returns members with scores within the specified range.
5. ZCARD key: Returns the number of members in the sorted set.
6. ZSCORE key member: Gets the score associated with a member.
Pub/Sub Commands
Redis supports a publish/subscribe messaging paradigm.
Pub/Sub Operations
1. PUBLISH channel message: Sends a message to a channel.
2. SUBSCRIBE channel: Subscribes to a channel for receiving messages.
3. UNSUBSCRIBE channel: Unsubscribes from a channel.
Transactions
Redis allows the execution of multiple commands as a single transaction with the following commands:
1. MULTI: Starts a transaction block.
2. EXEC: Executes all commands in the transaction block.
3. DISCARD: Discards all commands in the transaction block.
4. WATCH key: Marks a key to be monitored for changes.
Scripting
Redis supports Lua scripting for more complex operations.
Scripting Operations
1. EVAL script numkeys key1 key2 ...: Executes a Lua script with specified keys.
2. EVALSHA sha1 numkeys key1 key2 ...: Executes a Lua script using its SHA1 hash.
Server Commands
Server commands provide insights into the server’s state and performance.
Server Operations
1. INFO: Returns information and statistics about the server.
2. FLUSHDB: Deletes all keys in the current database.
3. FLUSHALL: Deletes all keys in all databases.
4. PING: Checks if the server is running.
5. SAVE: Saves the dataset to disk.
Conclusion
Utilizing the Redis commands cheat sheet can significantly enhance your efficiency when working with Redis. By mastering these commands, you will be better equipped to manage data, optimize performance, and implement complex features in your applications. Redis's simplicity and power provide a solid foundation for a wide range of use cases, from caching to real-time analytics, making it a critical tool for modern developers. Keep this cheat sheet handy as you navigate the rich set of commands Redis has to offer, and watch your productivity soar!
Frequently Asked Questions
What is a Redis command cheat sheet?
A Redis command cheat sheet is a quick reference guide that lists commonly used Redis commands along with their syntax and brief descriptions to help developers and database administrators efficiently use Redis.
What are some essential Redis commands for managing strings?
Essential Redis commands for managing strings include SET, GET, APPEND, and STRLEN, which allow you to set, retrieve, append to, and get the length of string values, respectively.
How do you create and manage lists in Redis?
You can create and manage lists in Redis using commands like LPUSH (to add elements to the beginning), RPUSH (to add elements to the end), LPOP (to remove elements from the beginning), and RPOP (to remove elements from the end).
What commands are used to handle sets in Redis?
Commands for handling sets include SADD (to add members), SREM (to remove members), SMEMBERS (to get all members), and SISMEMBER (to check if a member exists in a set).
How can you perform operations on hashes in Redis?
For hashes, you can use commands like HSET (to set a field in a hash), HGET (to get the value of a field), HDEL (to delete a field), and HGETALL (to retrieve all fields and values in a hash).
What are the commands to manage sorted sets in Redis?
Commands for managing sorted sets include ZADD (to add members with scores), ZREM (to remove members), ZRANGEBYINDEX (to get a range of members), and ZSCORE (to get the score of a member).
How can you work with Redis transactions?
You can work with Redis transactions using MULTI (to start a transaction), EXEC (to execute all commands in the transaction), and DISCARD (to discard the transaction).
What are some common Redis commands for key management?
Common key management commands include DEL (to delete a key), EXISTS (to check if a key exists), EXPIRE (to set a key's expiration time), and KEYS (to retrieve all keys matching a pattern).