@@ -84,17 +84,31 @@ def cache_write(app_id, data):
8484 return False
8585
8686
87+ def redis_connection ():
88+ """
89+ Parse redis config and connect.
90+ """
91+
92+ # try connection string, or default to separate REDIS_* env vars
93+ if "REDIS_URL" in os .environ :
94+ rds = redis .Redis .from_url (os .environ ["REDIS_URL" ])
95+ else :
96+ rds = redis .Redis (
97+ host = os .environ ["REDIS_HOST" ],
98+ port = os .environ ["REDIS_PORT" ],
99+ password = os .environ ["REDIS_PASSWORD" ],
100+ )
101+
102+ # return connection
103+ return rds
104+
105+
87106def redis_read (app_id ):
88107 """
89108 Read app info from Redis cache.
90109 """
91110
92- # parse redis config and connect
93- rds = redis .Redis (
94- host = os .environ ["REDIS_HOST" ],
95- port = os .environ ["REDIS_PORT" ],
96- password = os .environ ["REDIS_PASSWORD" ],
97- )
111+ rds = redis_connection ()
98112
99113 try :
100114 # get info from cache
@@ -130,12 +144,7 @@ def redis_write(app_id, data):
130144 Write app info to Redis cache.
131145 """
132146
133- # parse redis config and connect
134- rds = redis .Redis (
135- host = os .environ ["REDIS_HOST" ],
136- port = os .environ ["REDIS_PORT" ],
137- password = os .environ ["REDIS_PASSWORD" ],
138- )
147+ rds = redis_connection ()
139148
140149 # write cache data and set ttl
141150 try :
0 commit comments