EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis SETEX
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

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.

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more