Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Redis SETEX

Introduction to Redis SETEX

Redis Setex is used to set the key which holds the string value and set the key for a timeout after a specified number of seconds. The redis command is equivalent to the command of the set. It is atomic and it can be reproduced by using the commands contained within the exec or multi-block. For a faster alternative to the sequence, we can provide redis setex.

Key Takeaways

  • This command is used for setting the string value which was defined using the timeout value.
  • We can define any timeout value with redis setex. At the time of using it, we need to define three parameters i.e key name, timeout, and value.

What is Redis SETEX?

The setex operation is very common in the redis for using the cache. While using it we need to use TTL or get a command to retrieve the value from the key. Basically, the command will set the key which was given into the string value and the same has set into the time in number from the specified seconds. The set command is working as same as redis setex.

At the time of using setex, we need to provide the name of the key, timeout, and value parameter. We need to define the timeout value as per the time we have provided. As we know that redis is a very popular in-memory database which is also known as a data structure server. The redis contains strings, sets, hashes, and lists. In redis, we are using the redis setex command.

Redis SETEX Key and Value

We need to define the key and value. At the time of defining the example by using redis setex, we are using the parameter name as key and value. We also use the parameter name as a timeout in between the key and value.

The below syntax shows how we can use the key value parameter in setex as follows.

Syntax:

SETEX name_of_key timeout value

In the above syntax, the setex command is defining the key and value. The setex command we are defining holds the value of the string. The setex key is nothing but the key that we used to retrieve the value using the get command. The below example shows the setex key, in the below example, we are defining the setex key as redis_setex as follows.

Command:

SETEX redis_setex 10 redis
GET redis_setex

Output:

In the above example, we can see that we defined the key value as redis_setex and defined the value of the redis_setex key as redis. We can see that it will retrieve the value of the redis_setex key as redis.

At the time of using it, we need to define the key name as well as the value of the key name. Both parameters are important while using it. In the below example, we are not using a setex value, so it will return the error that we have provided the incorrect number of arguments.

Command:

SETEX redis_setex 20

Output:

In the below example, we are not using the setex key so it will give the error that the wrong number of arguments we have provided.

Command:

SETEX 20 redis

Output:

Redis SETEX Command

In the below example, we are defining the setex command key as redis_setex and the value as setex_command. Also, we are giving the timeout parameter value of 10 as follows. In the below example, we are also using the get method after defining the set method as follows.

Command:

SETEX redis_setex 10 setex_command
GET redis_setex

Output:

In the below example, we are defining the SETX command with TTL as follows. In the below example, we can see that we are defining the setex command key as redis_setex1 and the value as setex_command. Also, we are giving the timeout parameter value as 20 as follows.

Command:

SETEX redis_setex1 20 setex_command
TTL redis_setex1
GET redis_setex1

Output:

In the below example, we can see that we are using the setex command with value in the sentence as follows.

Command:

SETEX redis_setex1 20 "Redis setex command"
GET redis_setex1

Output:

Redis.Client.setex

For listing all the redis keys we need to use the keys command followed by the pattern. To use the redis client setex we need to create the upsert function and need to define the client.setex function as follows.

Code:

async function upsert(tab, data) {
let key = tab;
if (data && data.stud_id) {
key_name = key + '_' + data.stud_id;
}
client.setex (key_name, 10, JSON.stringify (data));
return true;
}

Output:

As we know that redis will accept the client connection by using a TCP port and using a linux socket. At the time of enabling the new connection of the client. At the time of accepting the client connection, we can perform the operation on the redis database server.

We can configure the max client connection limit to the redis database server. At the time of using redis client setex, we need to define the key and value. In the above example, we have defined the value of key-value as tab and data.

Examples of Redis SETEX

Given below are the examples mentioned:

Example #1

In the below example, we are using the setex command with get command as follows. In the below example we are using the key name as setex_example, also using the timeout as 30, and using the value as “Redis setex command example” as follows.

Command:

SETEX setex_example 30 "Redis setex command example"
get setex_example

Output:

Example #2

In the below example, we are using the setex command with get and TTL commands. Below we are using the key name as setex_example1, also using the timeout as 30, and using the value as “Redis setex command example with TTL” as follows.

Code:

SETEX setex_example1 30 "Redis setex command example with TTL"
TTL setex_example1
get setex_example

Output:

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of redis setex?

Answer: It is primarily used to set the string value that we have defined. We’ve defined the same value with a timeout.

Q2. Which parameter do we need?

Answer: We need to define the key name, value, and timeout parameter at the time of using setex in redis.

Q3. Which command we are using while defining the setex command?

Answer: We are using GET and TTL commands at the time of using the setex command in redis.

Conclusion

Basically, the command sets the given key into a string value and the time into a number starting from the specified seconds. It is used to set a key that previously held a string value and to set the key to timeout after a specified number of seconds.

Recommended Articles

This is a guide to Redis SETEX. Here we discuss the introduction, redis SETEX key, and value, Redis.Client.setex and examples. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version

The post Redis SETEX appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×