This guide will look at how to work with Redis keys and various commands to manage them.
Creating a Redis Key
The first step is to create a Redis key. Redis provides you with the SET command to create a Redis key and its associated value.
The syntax is as shown:
For example, to create a key called database and holds the value Redis, you can execute the command:
OK
The command returns a string “OK” if the command is executed successfully.
NOTE: When creating keys in Redis, you have to provide an associated value. If you give only the key name, Redis will return an error as shown:
(error) ERR wrong number of arguments for 'set' command
Deleting a Key
To delete a key in Redis, use the DEL command. The command takes the name of the key as the argument.
The syntax can be expressed as:
For example, to delete the key “database”, we can run the command as:
(integer) 1
The DEL command will return an integer value indicating the number of keys removed by the command.
If the provided key does not exist, the command will return an integer 0.
Rename a Key
To rename a key, use the RENAME command in Redis. This simple command takes the original key name and the new key name as the argument.
OK
Similar to the SET command, RENAME will return the string “OK” if the command is executed successfully.
Ensure the key that you wish to rename exists in the database. If not, Redis will return an error as shown:
(error) ERR no such key
Check if Key Exists
You can check if a key exists by using the EXISTS command. It takes the name of the key as the argument.
For example:
(integer) 1
The command will return an integer 1 indicating the key exists in the database and 0 if the key does not exist.
Relocate a Key
Suppose you want to move a key from a specific database to another. For that, you can use the MOVE command.
The command takes the key and the target database.
For example, to move the key “databases” from the database at index 0 to index 10, we can run the command:
(integer) 1
If the key exists in the source database, Redis will move it to the specified target database and return an integer 1. If not, the command returns integer 0.
Fetch Random Key
If you want to grab a random key from the Redis database, you can use the RANDOMKEY command. This command does not take any arguments but returns a random key.
"captains"
However, the key has to exist within the database.
Get Key Type
If you want to determine the data type that a specific key holds, you can use the TYPE command followed by the name of the key.
For example:
zset
The command shows the captains key holds a sorted set.
Ensure the key exists; otherwise, Redis will return a None type.
none
Expire a Key
You can set an expiration timer for a specific key. If the expiry duration elapses, the key is removed from the database.
For that, you can use the EXPIRE command followed by the key name and the expiry time for the key in seconds.
(integer) 1
This tells Redis to set the key “captains” lifetime to 60 seconds. After 60 seconds, the key is dropped from the database.
Get Remaining Key Duration
You can use the TTL command if you want to know how many seconds a specific key has before it expires.
10
Dump Values
You can use the DUMP command to get a serialized version of all the values stored in a key.
The resulting value is as shown below:
The above represents a serialized version of the values stored in the specified key.
Closing
This tutorial taught you how to work with keys and various commands to manage keys in a Redis database.
Thank you for reading!