1 | initial version |
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.