![]() |
In Redis, keys are the fundamental data structure used to identify, store, and retrieve data. Each key in Redis maps to a corresponding value, and Redis supports various data types for values like strings, hashes, lists, sets, sorted sets, and more. Redis keys are used to perform CRUD (Create, Read, Update, Delete) operations on the data. ![]() Redis Keys Important Topics for Redis KeysSyntax for Redis Keys:The syntax for using Redis keys in commands is quite straightforward:
Here, COMMAND represents the Redis command you want to perform (e.g., GET, SET, DEL, etc.), and key is the name of the key associated with the operation. Depending on the command, you may also provide additional arguments. Common Redis Key Commands and Examples:Here are some common Redis key-related commands along with examples: 1. SET:Sets the value of the specified key. Syntax:
Example:
Time Complexity: O(1) 2. GET:Retrieves the value associated with the specified key. Syntax:
Example:
Time Complexity: O(1) 3. DEL:Deletes one or more keys and their associated values. Syntax:
Example:
Time Complexity: O(N) 4. EXISTS:Checks if the specified key exists in the database. Syntax:
Example:
Time Complexity: O(1) 5. KEYS:Returns all keys matching a specific pattern. Syntax:
Example:
Time Complexity: O(N) 6. SCAN:Iterates through the keyspace using a cursor and provides an option to filter by a pattern and limit the number of results. Syntax:
Example:
Time Complexity: O(1) 7. RENAME:Renames a key to a new name. Syntax:
Example:
Time Complexity: O(1) 8. TYPE:Returns the data type of the value stored at the specified key. Syntax:
Example:
Time Complexity: O(1) 9. EXPIRE :Set a key’s time to live in seconds. Syntax:
Example:
Time Complexity: O(1) 10. TTL:Get the remaining time to live of a key in seconds. Syntax:
Example:
Time Complexity: O(1) 11. PERSIST :Remove the expiration from a key. Syntax:
Example:
Time Complexity: O(1) 12. DUMP:Serializes the value stored at the specified key and returns it as a binary-safe string. Syntax:
Example:
Time Complexity: O(1) 13. EXPIREAT:Sets an expiration timestamp (Unix timestamp) on a key. The key will expire and be automatically deleted at the specified timestamp. Syntax:
Example:
Time Complexity: O(1) 14. PEXPIRE:Sets an expiration time on a key in milliseconds. The key will expire and be automatically deleted after the specified number of milliseconds. Syntax:
Example:
Time Complexity: O(1) 15. PEXPIREAT:Sets an expiration timestamp in milliseconds (Unix timestamp in milliseconds) on a key. The key will expire and be automatically deleted at the specified timestamp. Syntax:
Example:
Time Complexity: O(1) 16. MOVE:Moves the specified key from the current database to the specified destination database (db). Syntax:
Example:
Time Complexity: O(1) 17. PTTL:Returns the remaining time to live of a key in milliseconds. If the key does not have an associated expiration, it returns -1. If the key does not exist, it returns -2. Syntax:
Example:
Time Complexity: O(1) 18. RANDOMKEY:Returns a random key from the current database. Syntax:
Example:
Time Complexity: O(1) 19. RENAMENX:Renames a key to newkey only if newkey does not already exist. If newkey exists, the operation has no effect. Syntax:
Example:
Time Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
System Design |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |