Usually, the GET and SET commands are used to store and retrieve string-type values stored at a given Redis key. These two commands operate with constant time complexity.
In addition, the GETEX command is available to read string values stored at a given key. But this command is associated with a parallel write operation, as well. This guide will focus on the GETEX command syntax, its options, and use cases.
The time to live(TTL) value is the expiration time of a given piece of data. Usually, a TTL can be associated with database rows. A timeout can be set with Redis keys, as well. So, a Redis client can check the remaining seconds that a given key will be available in the data store. Redis offers a TTL command to check the time to live value of a given key. If no timeout is associated with a specified key, the TTL command returns -1. We will use the TTL command in the next sections to check the remaining time of a given key.
As highlighted, the GETEX command is more similar to the GET command, except that it can be used to set an expiration time for the specified key. Furthermore, a command option can remove a timeout value from a given key. The GETEX command syntax is as follows:
key: This is the key that points to a string value.
The following optional arguments can be used to set a timeout in seconds or milliseconds.
EX: This will set the expiration time in seconds for a given key.
PX: The key timeout value will be set in milliseconds.
In addition, we can specify a UNIX timestamp value in seconds or milliseconds using the following command options EXAT and PXAT:
EXAT: The expiration time will be set in the UNIX timestamp format, and the timestamp will be in seconds.
PXAT: The timeout for a given Redis key will be set as a UNIX timestamp in milliseconds.
PERSIST: By specifying this, you can remove the time to live value associated with a specified key.
The GETEX command returns the value of the specified key. This command works only with string-type values but not hashes, lists, sorted sets, etc. If you specify a key associated with a non-string value, the command will return an error. In addition, if the key is already expired or doesn’t exist, the command will return nil.
In the following section, we will cover the practical usage of the GETEX command.
A web hosting company is providing a password recovery feature for registered users. As a security measure, they send a new URL link to the user, which will expire within 5 minutes. The company has been using the Redis data store for a long time and uses the GETEX command to set a time to live value for a given user.
Let’s assume that a user with user_id 1000 requests a password recovery. So, the recovery URL can be stored at the key RecoveryURL:UserID:1000. We can use the SET command to store the recovery URL as follows:
Let’s check whether the string URL is properly stored at the key RecoveryURL:UserID:1000. We can use the GETEX command as follows:
The output of the previous command is shown in the following:
If we have used the GET command instead of the GETEX command, the output will be the same.
Let’s use the TTL command to check whether an expiration time is associated with this key.
As expected, the -1 has been returned by the TTL command, which means no expiry time is associated with the specified key.
As per this example, the hosting company needs to set a 5 minutes timeout for the recovery URL. So, we will be using the EX command option. Since 5 minutes is 300 seconds, we can construct the GETEX command as follows:
Let’s run the TTL command a few times to inspect the remaining time that the key will exist in the data store.
As expected, the time is decreasing, and eventually, the RecoveryURL:UserID:1000 key will be removed. The following is the output after 5 minutes:
The -2 has been returned by the command, which means the key is expired and deleted from the data store.
Sometimes, you might need to remove the timeout value associated with a given key. So, the PERSIST command option can be used to delete a TTL value related to a specified key.
Let’s create another sample key and assign a string value to it. In this case, we will specify the TTL value in milliseconds by passing the PX option.
Next, we will be checking the remaining time as follows:
As expected, the TTL is decreasing. Let’s call the GETEX command with the PERSIST option as follows:
Ideally, this would remove the TTL associated with this key. Let’s inspect the TTL for the key KeytoTestPersist.
The TTL command returned the -1, which means no TTL is associated with the key KeytoTestPersist.
Similarly, the GETEX command can be used to set the expiry time for keys in UNIX timestamp.
In summary, the Redis GETEX command can be used to retrieve the string values stored at a given key as the GET command does. The only difference with the GETEX command is that it supports setting and removing expiry times for a given key. It supports several optional arguments to specify the expiry time in seconds, milliseconds, and UNIX-timestamp seconds or milliseconds. As discussed, the command provides the PERSIST option to remove associated TTL for a given key. Overall, the GETEX command operates with constant time complexity.