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.
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
Asked: 2021-12-11 11:00:00 +0000
Seen: 15 times
Last updated: Aug 18 '22
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?