From 9f5c83865ebcca3293d8f30dea6ea6fa8ed5abc2 Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 10 Nov 2023 23:49:33 -0500 Subject: Moved to Redis Cloud --- chat/chat_cache.py | 12 +++++++++++- middleware/uuidmiddleware.py | 10 +++++++++- thatcomputerscientist/settings.py | 13 ++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/chat/chat_cache.py b/chat/chat_cache.py index 43354ef7..8c107a6c 100644 --- a/chat/chat_cache.py +++ b/chat/chat_cache.py @@ -2,7 +2,17 @@ import json import redis -r = redis.Redis(host='localhost', port=6379, db=0) +import os +from dotenv import load_dotenv + +load_dotenv() + +r = redis.Redis( + host=os.getenv('REDIS_HOST'), + port=os.getenv('REDIS_PORT'), + password=os.getenv('REDIS_PASSWORD'), + db=0 +) def handle_connect(): # increase number of connected users diff --git a/middleware/uuidmiddleware.py b/middleware/uuidmiddleware.py index a6dd1ca3..42fe5903 100644 --- a/middleware/uuidmiddleware.py +++ b/middleware/uuidmiddleware.py @@ -2,9 +2,17 @@ import json import uuid import redis +import os +from dotenv import load_dotenv -redis_instance = redis.StrictRedis(host='localhost', port=6379, db=0) +load_dotenv() +redis_instance = redis.StrictRedis( + host=os.getenv('REDIS_HOST'), + port=os.getenv('REDIS_PORT'), + password=os.getenv('REDIS_PASSWORD'), + db=0 +) class UserUUIDMiddleware: # assign a uuid to the user if they don't have one diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py index 8632a951..1ca6b2df 100644 --- a/thatcomputerscientist/settings.py +++ b/thatcomputerscientist/settings.py @@ -24,7 +24,13 @@ BASE_DIR = Path(__file__).resolve().parent.parent import redis -r = redis.Redis(host='localhost', port=6379, db=0) +# r = redis.Redis(host='localhost', port=6379, db=0) +r = redis.Redis( + host=os.getenv('REDIS_HOST'), + port=os.getenv('REDIS_PORT'), + password=os.getenv('REDIS_PASSWORD'), + db=0 +) r.set('n_connected_lc_users', 0) # Quick-start development settings - unsuitable for production @@ -140,7 +146,7 @@ CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { - "hosts": [("127.0.0.1", 6379)], + "hosts": [(f'redis://:{os.getenv("REDIS_PASSWORD")}@{os.getenv("REDIS_HOST")}:{os.getenv("REDIS_PORT")}')], }, }, } @@ -177,9 +183,10 @@ AUTH_PASSWORD_VALIDATORS = [ CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": "redis://127.0.0.1:6379/1", + "LOCATION": f"redis://{os.getenv('REDIS_HOST')}:{os.getenv('REDIS_PORT')}", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", + "PASSWORD": os.getenv('REDIS_PASSWORD'), } } } -- cgit v1.2.3