EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Redis Tutorial Redis SETEX
 

Redis SETEX

Updated February 17, 2023

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.

 

 

REDIS SETEX

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Redis SETEX 1

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:

Redis SETEX 2

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 3

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:

Redis SETEX 4

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:

Redis SETEX 5

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 SETEX 6

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:

need to create the upsert function

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:

timeout as 30

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:

get and TTL command

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW