Skip to content

Commit

Permalink
Merge pull request #43 from sxalexander/pr-allow-redis-url
Browse files Browse the repository at this point in the history
Adds REDIS_URL support and updates the docs
  • Loading branch information
jonakoudijs authored Feb 25, 2023
2 parents b41fa22 + 3cbef29 commit b38ecdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ REDIS_HOST="your.redis.host.example.com"
REDIS_PORT=6379
REDIS_PASSWORD="YourRedisP@ssword!"
# OR, if your host provides a Connection URL
# (see: https://redis-py.readthedocs.io/en/stable/#quickly-connecting-to-redis)
REDIS_URL="redis://YourUsername:YourRedisP@[email protected]:6379"
# deta
DETA_BASE_NAME="steamcmd"
DETA_PROJECT_KEY="YourDet@ProjectKey!"
Expand Down
33 changes: 21 additions & 12 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,31 @@ def cache_write(app_id, data):
return False


def redis_connection():
"""
Parse redis config and connect.
"""

# try connection string, or default to separate REDIS_* env vars
if "REDIS_URL" in os.environ:
rds = redis.Redis.from_url(os.environ["REDIS_URL"])
else:
rds = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
password=os.environ["REDIS_PASSWORD"],
)

# return connection
return rds


def redis_read(app_id):
"""
Read app info from Redis cache.
"""

# parse redis config and connect
rds = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
password=os.environ["REDIS_PASSWORD"],
)
rds = redis_connection()

try:
# get info from cache
Expand Down Expand Up @@ -130,12 +144,7 @@ def redis_write(app_id, data):
Write app info to Redis cache.
"""

# parse redis config and connect
rds = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
password=os.environ["REDIS_PASSWORD"],
)
rds = redis_connection()

# write cache data and set ttl
try:
Expand Down

0 comments on commit b38ecdf

Please sign in to comment.