| by Arround The Web | No comments

Redis EXPIRE Hash Key

Redis hashes are a special kind of data type which is a lot more similar to the JSON object, Java HashMap, or a Python dictionary. Furthermore, it is a collection of field-value pairs that can be used to model domain objects. Redis hash data structure is extremely memory efficient where each hash key can store up to four billion field-value pairs. Most importantly, the fundamental hash operations like HSET, HGET, HMGET, etc. operate on constant time complexity.


Redis hash keys have infinite time to live (TTL) which means that they are persistent they are deleted explicitly using commands like DEL. In this article, we will focus on setting the TTL for Redis hashes using the EXPIRE command.

Redis EXPIRE Command

The EXPIRE command is used to set a timeout on a given key of a Redis hash, set, list, etc. The Redis key is deleted from the database if the timeout expires. Most importantly, the timeout is uncleared until the contents of the key are deleted or overwritten. Altering the values associated with a key does not affect the expiry time.

The syntax of the EXPIRE command is as follows:

EXPIRE key expiry_time_seconds [ NX | GX | GT | LT ]

 
key: The key of the Hash, List, or Set that you need to set a timeout.

expirty_time_seconds: The timeout value in seconds.

Several optional arguments are accepted by the EXPIRE command.

NX: The timeout value is set only if the specified key has no expiry already.

XX: When the specified key has an existing timeout value, the new expiry is set.

GT: If the new timeout value is greater than the existing one, the new expiry is set.

LT: The new timeout value is set if the existing one is greater than the new one.

Most importantly, the EXPIRE command operates on constant time complexity. The integer 1 is returned if the command execution is successful. If the operation fails due to wrong arguments or non-existing keys, 0 is returned.

We will use the EXPIRE command on hashes to set an expiry time as shown in the following section:

Expire Redis Hash Using the EXPIRE Command

Let’s assume that a session information per user is stored in a Redis hash session:id:1000:user:10. We can use the HMSET command to create a Redis hash with multiple field-value pairs as follows:

hmset session:id:1000:user:10 username "jae" cookie "yes" password "389Ysu2"

 
Let’s inspect the created hash using the HGETALL command.

hgetall session:id:1000:user:10

 

In addition, the session expires after 10 seconds if the user is idle for more than 60 seconds. Session expiration is achieved by setting the expiry time for the hash that stores the session information.

We can use the EXPIRE command as follows:

expire session:id:1000:user:10 10

 
As mentioned, the timeout value is set to 10 seconds.


As expected, the return value is 1 which means that the TTL is set successfully for the hash. Let’s check the time left before the hash key is removed from the Redis store. The TTL command can be used as follows:

ttl session:id:1000:user:10

 

As shown in the output, three seconds are left before removing the hash automatically. After 10 seconds, the TTL command output is as follows:


As the -2 integer reply is indicated, the hash doesn’t exist.

Set Timeout Based on the Existence of an Expiry Time

The EXPIRE command accepts NX and XX arguments to set a new timeout based on the existence of expiry for a specified hash. Let’s create a new hash with the noTimeOut key.

hmset noTimeOut name "test"

 
Let’s try to set a new expiry to the previous hash. Additionally, we pass the XX argument to the EXPIRE command as well.

expire noTimeOut 15 XX

 
Since we specify the XX option in the command, the expiry time won’t be set. The XX option doesn’t allow you to set a new expiry time if there is no existing timeout associated with the specified hash key.


If we use the NX option, the timeout value is set to 15.

expire noTimeOut 15 NX

 

The EXPIRE command returns the integer 1 reply which means that the timeout is set properly.

Set Timeout Based on the Existing Timeout Value

The GT and LT options can be used to set the hash expiry time based on the existing timeout length.

Let’s create a new hash called hashWithTimeout.

hmset hashWithTimeout field1 value1

 
Next, we set a 200 seconds expiry time for the hash.

expire hashWithTimeout 200

 
Let’s try to set a new timeout of 100 seconds for the hash along with the GT option as follows:

expire hashWithTimeout 100 GT

 
Since the GT option has been specified, the EXPIRE command will check if the new timeout value is greater than the existing one and set the new expiry time. In this example, the new timeout is not greater than the existing timeout. Hence, the command will not set the new expiry time and 0 will be returned.


Let’s use the LT option instead of GT. Since the new expiry time is lower than the current one, the following command should successfully set the new timeout.

expire hashWithTimeout 100 LT

 

Conclusion

In short, the Redis EXPIRE command is used to set a TTL value for a given key. By default, the Redis hash keys are not associated with any timeout which is called non-volatile. As discussed, the EXPIRE command is used to set a timeout value on the Redis hash. Usually, the hash is deleted from the Redis data store after the amount of time specified as a timeout value. As shown in the examples, the EXPIRE command accepts some optional arguments like XX, NX, GT, and LT to set the hash expiry based on a condition.

Share Button

Source: linuxhint.com

Leave a Reply