Ask Your Question
4

How can I use the Python redis module to set the expiration time?

asked 2021-12-11 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-08-18 23:00:00 +0000

huitzilopochtli gravatar image

You can set the expiration time using the expire method in the Python Redis module. Here's an example:

import redis

# create a Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)

# set a key-value pair with an expiration time of 60 seconds
redis_client.set('my_key', 'my_value', ex=60)

# get the value of the key (this will work only within 60 seconds)
value = redis_client.get('my_key')
print(value)

In this example, we set a key-value pair with the ex parameter set to 60, which means the key will expire in 60 seconds. After setting the key-value pair, we retrieve the value using the get method. If we try to retrieve the value after 60 seconds, it will return None because the key has expired.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-11 11:00:00 +0000

Seen: 15 times

Last updated: Aug 18 '22