Redis Cheatsheet – Essential Commands for Database Management

Redis Commands

Redis, an in-memory data structure store, is renowned for its speed and versatility. This cheatsheet compiles essential Redis commands that help you manage your database effectively, whether you’re a beginner or an experienced user.

1. DEL

Usage: `DEL key`
Description: Deletes the specified key. If the key does not exist, it is ignored.
Example: `DEL mykey`

2. DUMP

Usage: `DUMP key`
Description: Serializes the value stored at the specified key, returning it as a binary safe string.
Example: `DUMP mykey`

3. EXISTS

Usage: `EXISTS key`
Description: Checks if a key exists in the database, returning 1 if it does and 0 if it doesn’t.
Example: `EXISTS mykey`

4. EXPIRE

Usage: `EXPIRE key seconds`
Description: Sets a timeout on the specified key, after which it will be automatically deleted.
Example: `EXPIRE mykey 60`

5. EXPIREAT

Usage: `EXPIREAT key timestamp`
Description: Sets a timeout on the specified key using an absolute Unix timestamp.
Example: `EXPIREAT mykey 1633072800`

6. KEYS

Usage: `KEYS pattern`
Description: Returns all keys matching a specified pattern (note: can be slow on large datasets).
Example: `KEYS user:*`

7. MIGRATE

Usage: `MIGRATE host port key destination-db timeout`
Description: Transfers an item from one Redis instance to another.
Example: `MIGRATE 192.168.1.1 6379 mykey 1 5000`

8. MOVE

Usage: `MOVE key db`
Description: Moves a key from the current database to a specified database.
Example: `MOVE mykey 1`

9. OBJECT

Usage: `OBJECT subcommand [arguments]`
Description: Inspects various aspects of an item (e.g., its encoding, ID).
Example: `OBJECT ENCODING mykey`

10. PERSIST

Usage: `PERSIST key`
Description: Removes the expiration from a key, making it persistent.
Example: `PERSIST mykey`

11. PEXPIRE

Usage: `PEXPIRE key milliseconds`
Description: Sets a timeout on the specified key in milliseconds.
Example: `PEXPIRE mykey 1500`

12. PEXPIREAT

Usage: `PEXPIREAT key milliseconds-timestamp`
Description: Sets a timeout on the specified key using a millisecond timestamp.
Example: `PEXPIREAT mykey 1633072800000`

13. PTTL

Usage: `PTTL key`
Description: Returns the remaining time to live of a key in milliseconds.
Example: `PTTL mykey`

14. RANDOMKEY

Usage: `RANDOMKEY`
Description: Returns a random key from the current database.
Example: `RANDOMKEY`

15. RENAME

Usage: `RENAME oldkey newkey`
Description: Renames a key. If the new key already exists, it will be overwritten.
Example: `RENAME mykey newkey`

16. RENAMENX

Usage: `RENAMENX oldkey newkey`
Description: Renames a key only if the new key does not already exist.
Example: `RENAMENX mykey newkey`

17. RESTORE

Usage: `RESTORE key ttl serialized-value`
Description: Deserializes a value into a key, setting its expiration.
Example: `RESTORE mykey 0 serialized_value`

18. SCAN

Usage: `SCAN cursor [MATCH pattern] [COUNT count]`
Description: Iterates over keys in the database, allowing for pattern matching.
Example: `SCAN 0 MATCH user:* COUNT 10`

19. SORT

Usage: `SORT key [BY pattern] [GET pattern] [ASC|DESC] [LIMIT offset count] [STORE destination]`
Description: Retrieves or stores a sorted copy of a list, set, or sorted set.
Example: `SORT mylist ASC`

20. TTL

Usage: `TTL key`
Description: Returns the remaining time to live of a key in seconds.
Example: `TTL mykey`

21. TYPE

Usage: `TYPE key`
Description: Returns the data type of the value stored at the key.
Example: `TYPE mykey`

Conclusion

This Redis cheatsheet provides a quick reference to some of the most commonly used commands for database management. By mastering these commands, you can effectively manipulate your data and optimize performance in your Redis instance.

Whether you are managing simple key-value pairs or more complex data structures, these commands will empower you to get the most out of Redis.

You may also like:

Related Posts

Leave a Reply