Skip to content

Commit b38ecdf

Browse files
authored
Merge pull request #43 from sxalexander/pr-allow-redis-url
Adds REDIS_URL support and updates the docs
2 parents b41fa22 + 3cbef29 commit b38ecdf

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ REDIS_HOST="your.redis.host.example.com"
101101
REDIS_PORT=6379
102102
REDIS_PASSWORD="YourRedisP@ssword!"
103103
104+
# OR, if your host provides a Connection URL
105+
# (see: https://redis-py.readthedocs.io/en/stable/#quickly-connecting-to-redis)
106+
REDIS_URL="redis://YourUsername:YourRedisP@ssword!@your.redis.host.example.com:6379"
107+
104108
# deta
105109
DETA_BASE_NAME="steamcmd"
106110
DETA_PROJECT_KEY="YourDet@ProjectKey!"

src/functions.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
87106
def 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

Comments
 (0)