Search code examples
pythonredisconnection-string

TTL setting for Redis inside Connection String, is it possible?


I am doing a maintenance on several Python applications, which use connections to Redis services. Due to an infrastructure need, I am reducing the number of variables we will have in the environment settings as much as possible for all applicaitons. Today, the application uses several parameters, for example:

REDIS_PRIMARY_HOST= "localhost"
REDIS_PRIMARY_PORT= "6379"
REDIS_PRIMARY_PASSWORD= "password"
REDIS_PRIMARY_TTL_SECONDS = "1"
REDIS_DATABASE = "0"

We want to make a change and only use a connection string and another variable, such as:

REDIS_PRIMARY_CONNECTION_STRING = "redis://:password@localhost:6379?db=<database_number>&decode_responses=True&health_check_interval=2"
REDIS_DATABASE_NUMBER = "0"

However, via Connection String, I couldn't find in the documentation a way to set the TTL (Time To Live) as default to a value. Either I do this directly in the code, or I will have to extract the key parameter and define it at run time. Is there any way to set the Redis Time To Live by default, in the connection string?


Solution

  • Redis doesn't support a global TTL. You will need to set it individually for each key.