Skip to content

Commit

Permalink
Improved Redis expiration set
Browse files Browse the repository at this point in the history
  • Loading branch information
jonakoudijs committed Sep 22, 2023
1 parent 4791119 commit 5c8d0de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'
services:
web:
build: .
command: ["--port", "$PORT", "main:app", "--reload"]
command: "gunicorn main:app --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --reload"
ports:
- "8000:8000"
volumes:
Expand Down
13 changes: 9 additions & 4 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ def redis_connection():
# 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:
elif "REDIS_PASSWORD" in os.environ:
rds = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
password=os.environ["REDIS_PASSWORD"],
password=os.environ["REDIS_PASSWORD"]
)
else:
rds = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"]
)

# return connection
Expand Down Expand Up @@ -152,8 +157,8 @@ def redis_write(app_id, data):
data = json.dumps(data)

# insert data into cache
rds.set(app_id, data)
rds.expire(app_id, os.environ["CACHE_EXPIRATION"])
expiration = int(os.environ["CACHE_EXPIRATION"])
rds.set(app_id, data, ex=expiration)

# return succes status
return True
Expand Down

0 comments on commit 5c8d0de

Please sign in to comment.