Sobes.tech
Junior

Is TTL (time to live) supported in Redis?

sobes.tech AI

Answer from AI

Yes, Redis supports key expiration times (TTL, Time To Live). This means you can set a duration after which the key will automatically be removed from the database. TTL is set using the EXPIRE command or when creating a key with commands that support setting a TTL.

Example of setting TTL in Python using the redis-py library:

import redis

r = redis.Redis(host='localhost', port=6379, db=0)
r.set('mykey', 'value')
r.expire('mykey', 60)  # the key 'mykey' will live for 60 seconds

After 60 seconds, the key 'mykey' will be automatically removed from Redis.