A Complete Guide to Redis Keys
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 Keys
- Syntax for Redis Keys:
- Common Redis Key Commands and Examples:
Syntax for Redis Keys:
The syntax for using Redis keys in commands is quite straightforward:
COMMAND key [arg1 arg2 …]
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:
SET key value
Example:
SET user:123 name “John Doe” email “john@example.com” age 30
Time Complexity: O(1)
2. GET:
Retrieves the value associated with the specified key.
Syntax:
GET key
Example:
GET user:123:name
Time Complexity: O(1)
3. DEL:
Deletes one or more keys and their associated values.
Syntax:
DEL key [key …]
Example:
DEL username
Time Complexity: O(N)
4. EXISTS:
Checks if the specified key exists in the database.
Syntax:
EXISTS key
Example:
EXISTS user:123
Time Complexity: O(1)
5. KEYS:
Returns all keys matching a specific pattern.
Syntax:
KEYS pattern
Example:
KEYS user:*
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:
SCAN cursor [MATCH pattern] [COUNT count]
Example:
SCAN 0 MATCH user:* COUNT 10
Time Complexity: O(1)
7. RENAME:
Renames a key to a new name.
Syntax:
RENAME oldkey newkey
Example:
RENAME user:123:name user:123:fullname
Time Complexity: O(1)
8. TYPE:
Returns the data type of the value stored at the specified key.
Syntax:
TYPE key
Example:
TYPE user:123:age
Time Complexity: O(1)
9. EXPIRE :
Set a key’s time to live in seconds.
Syntax:
EXPIRE key seconds
Example:
EXPIRE mykey 60
Time Complexity: O(1)
10. TTL:
Get the remaining time to live of a key in seconds.
Syntax:
TTL key
Example:
TTL mykey
Time Complexity: O(1)
11. PERSIST :
Remove the expiration from a key.
Syntax:
PERSIST key
Example:
PERSIST mykey
Time Complexity: O(1)
12. DUMP:
Serializes the value stored at the specified key and returns it as a binary-safe string.
Syntax:
DUMP key
Example:
> SET mykey “Hello, Redis!”
OK
> DUMP mykey
“\x00\x0bHello, Redis!\x00”
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:
EXPIREAT key timestamp
Example:
> SET mykey “Hello, Redis!”
OK
> EXPIREAT mykey 1632144000 # Expires on September 21, 2021
(integer) 1
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:
PEXPIRE key milliseconds
Example:
> SET mykey “Hello, Redis!”
OK
> PEXPIRE mykey 60000 # Expires in 60 seconds (60000 milliseconds)
(integer) 1
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:
PEXPIREAT key milliseconds-timestamp
Example:
> SET mykey “Hello, Redis!”
OK
> PEXPIREAT mykey 1632144000000 # Expires on September 21, 2021, in milliseconds
(integer) 1
Time Complexity: O(1)
16. MOVE:
Moves the specified key from the current database to the specified destination database (db).
Syntax:
MOVE key db
Example:
> SET mykey “Hello, Redis!”
OK
> MOVE mykey 1 # Move to database 1
(integer) 1
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:
PTTL key
Example:
> SET mykey “Hello, Redis!”
OK
> PEXPIRE mykey 60000 # Expires in 60 seconds (60000 milliseconds)
(integer) 1
> PTTL mykey
(integer) 59959
Time Complexity: O(1)
18. RANDOMKEY:
Returns a random key from the current database.
Syntax:
RANDOMKEY
Example:
> SET key1 “Value1”
OK
> SET key2 “Value2”
OK
> RANDOMKEY
“key2”
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:
RENAMENX key newkey
Example:
> SET mykey “Hello, Redis!”
OK
> RENAMENX mykey newkey
(integer) 1
> GET mykey
(nil)
> GET newkey
“Hello, Redis!”
Time Complexity: O(1)
Contact Us