Storing Strings in Redis: Insertion and Retrieval Commands
Learn about how we can store Strings in Redis.
We'll cover the following
The simplest form of data that can be stored in the Redis database is String. We can store static HTML pages in the Redis database where the key is some name used to identify the page and value is the HTML String. We will look at all the commands used to store and fetch records from the Redis database when the value type is String.
Please practice all the commands in the terminal at the end of this lesson.
SET
command
We can store a record in Redis using the SET
command. The syntax of this command is:
SET key value
Let’s say we need to save the name of a company and its stock price in Redis. In the example below, we are storing the stock price of Apple in Redis. When we execute a command in Redis, OK is returned, which means that the command was executed successfully.
GET
command
We can get the stock value of Apple using the GET
command.
GET key
In the example below, we are getting the stock price of Apple.
If we run the SET
command again for Apple, with a different value, then the record in Redis is updated.
SETEX
command
This command is used if we need to set a certain time after which the key will be removed from the database. The time provided is in seconds.
SETEX key seconds value
In the example below, we are storing the stock price for Microsoft. We are providing the time as 40 seconds. When we try to get this key before 40 seconds, then we get the data. We will try again after 40 seconds, and the data will not be returned. The two GET commands demonstrate this case.
Instead of using the SETEX
command, we can also use the SET
command with the EX
option as shown below.
PSETEX
command
This command is similar to SETEX
command. The only difference is that the time provided is in milliseconds.
PSETEX key millisecond value
In the example below, we are storing the stock price of Amazon with 20,000 milliseconds as the expiry time.
Instead of using the PSETEX
command, we can also use the SET
command with the PX
option as shown below.
SET key value PX milliseconds
Please try these commands in the terminal below:
SET Apple 300
GET Apple
SETEX Microsoft 40 145
PSETEX Amazon 2000 245
SETNX
command
The SETNX
command is used if a key is already present. If a key is already present and we run SET
again, then the value is updated. If we need to avoid this, we can use SETNX
. The syntax of this command is:
SETNX key value
Instead of using the SETNX
command, we can use the SET
command with the NX
option.
In the example below, the stock price of apple is already available in the database. We can set it again with the NX
option so that the value is not updated.
STRLEN
command
This command is used if we need to find the length of the value for a particular key. The syntax of this command is:
STRLEN key
MSET
command
If we need to save multiple records in a single command, we can use the MSET
command. In the example below, we are setting the stock prices of multiple companies in a single command. The syntax of this command is:
MSET key1 value1 key2 value2
MGET
command
If we need to get the value of multiple keys in a single command, we can use the MGET
command. In the example below, we are getting the stock prices of multiple companies in a single command.
MGET key1 key2
Please try these commands in the terminal below:
SET Apple 300
GET Apple
SETNX Apple 240 (this will not update the value of Apple)
STRLEN Apple
MSET Google 450 fiserv 230
MGET Google fiserv