Redis Key Value Pair Command Basic Tutorial - Part 2

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’ve spent any time working with Redis, you know that key-value pairs are used to store all the data in the system. Knowing how to access and manipulate these key-value pairs is essential to use Redis effectively. In the first installment of this two-part series, we looked at some common Redis key value pair commands. This article will pick up where we left off and continue our overview of basic Redis commands.

Prerequisites

Before we begin executing Redis key value pair commands, let’s go over the prerequisites that are essential for this tutorial:

  • You’ll need to ensure that Redis is properly installed and configured. You can download Redis on their website Redis.io
  • Make sure that your Redis server is running in the background.

Redis RENAME Command

The Redis RENAME command does exactly what its name implies– it will rename a Redis key.

1
2
3
4
5
6
7
127.0.0.1:6379> SET object "ball"
OK
127.0.0.1:6379> RENAME object bounce
OK
127.0.0.1:6379> GET bounce
"ball"
127.0.0.1:6379>

In the code shown above, we set a key named “object” and set its value to “ball”. We then rename “object”, giving it the new name “bounce”.

Next, we verify that our operation was successful using the “GET” command. It returns the value “ball” that we initially set to the key “object”.

Redis RENAMENX Command

The Redis RENAMENX command allows us to rename a key provided that a key with the new name does not already exist. Redis will not execute the command if such a key exists.

1
2
3
4
5
127.0.0.1:6379> SET toy "airplane"
OK
127.0.0.1:6379> RENAMENX toy bounce
(integer) 0
127.0.0.1:6379>

In the example above, we created a key-value pair named “toy” with a value of “airplane”. We then try to perform the RENAMENX command against the key named “toy”, giving it the new name “bounce”. Keep in mind that we created a key named “bounce” in our previous example. Redis notices this and will not run the command, returning (integer) 0 as the response since the key “bounce” already exists.

Redis GETRANGE Command

The GETRANGE command returns the substring of a specified string value that is determined by offsets indicating its start and end points.

1
2
3
4
5
127.0.0.1:6379> SET pstring "The quick brown fox"
OK
127.0.0.1:6379> GETRANGE pstring 0 3
"The "
127.0.0.1:6379>

Let’s take a closer look at what’s happening in these commands. First, we set a key named “pstring” with value of “The quick brown fox”. Then we perform GETRANGE. This command takes the parameter “0” as its starting point and the parameter “3” as its ending point. Redis returns the string “The”, which is the substring located between the specified starting and ending points.

Redis GETSET Command

As we discussed in the first part of this two-part series, GETSET will automatically set a key to a value and will return the old value.

1
2
3
4
5
127.0.0.1:6379> GETSET pstring "jump over the lazy dog"
"The quick brown fox"
127.0.0.1:6379> GET pstring
"jump over the lazy dog"
127.0.0.1:6379>

Notice how Redis returned the old value of the key “pstring”: the string “The quick brown fox”. We can verify that we did successfully replace the value of “pstring” using the command GET pstring.

Redis SETEX Command

The Redis SETEX command allows us to set a key to hold a given value for a certain period of time in seconds.

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> SET key1 "hello"
OK
127.0.0.1:6379> EXPIRE key1 10
(integer) 1
127.0.0.1:6379> ttl key1
(integer) 8
127.0.0.1:6379> ttl key1
(integer) 4
127.0.0.1:6379>

The code shown above will achieve the same result as SETEX, but it requires more commands to execute.

In the following command, we’ll use the SETEX command, and you can see the difference:

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> SETEX key1 20 "hello"
OK
127.0.0.1:6379> ttl key1
(integer) 18
127.0.0.1:6379> ttl key1
(integer) 11
127.0.0.1:6379> ttl key1
(integer) 8
127.0.0.1:6379>

Redis PSETX Command

The Redis PSETX is very similar in functionality to SETEX, but it only uses milliseconds:

1
2
3
4
5
6
7
127.0.0.1:6379> PSETEX key1 5000 "Hello"
OK
127.0.0.1:6379> pttl key1
(integer) 1249
127.0.0.1:6379> pttl key1
(integer) -2
127.0.0.1:6379>

In this example, we set a value for “key1” that will be held under 5000 milliseconds. As we execute the command PTTL key1, we can see that it returns a decreasing value of milliseconds until we get a -2 response, which tells us that the value already expired. This releases the key and removes the value that was temporarily occupying “key1”.

Redis PERSIST

This Redis command will remove an existing timeout on a key. Let’s look at an example similar to the one in the previous section. This time, however, we will stop the expiration timer while it’s in effect:

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> PSETEX key1 20000 "hello"
OK
127.0.0.1:6379> PTTL key1
(integer) 9763
127.0.0.1:6379> PERSIST key1
(integer) 1
127.0.0.1:6379> PTTL key1
(integer) -1
127.0.0.1:6379> GET key1
"hello"

The code shown above sets a timer of 20 seconds for the value “hello” to occupy “key1”. We can see that the timer is at 9731 milliseconds when we execute the PERSIST command. After executing PERSIST, we execute the PTTL key1 command again to check if the timer has stopped; we receive a result of “-1”, which means that the timer was terminated.

We then perform the GET key1 command. The response shows us that the value “hello” persists for the key “key1”, even though the timer that was originally set with PSETEX has expired.

Conclusion

In Redis, data is stored in key-value pairs, so it’s essential to know how to access and manipulate these pairs using the basic Redis commands. This second installment in our two-part series continued our overview of Redis key value pair commands, covering RENAME, GETSET, SETEX and many more. With the information provided in this tutorial, you’ll be prepared to work with key-value pairs in your own Redis implementation.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.