From 0215680ea5ea96a380eb57d8309e8b7d9124651c Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 12 Sep 2023 19:42:32 +0800 Subject: [PATCH] Fixed issue of not connecting to redis instance and added caching for courses --- linux-requirements.txt | Bin 2354 -> 2396 bytes stude/config/settings.py | 21 ++++++++++++++++++++- stude/courses/views.py | 6 ++++++ stude/wait_for_redis.py | 1 - 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/linux-requirements.txt b/linux-requirements.txt index 3377fcf69641374f6adae2726e65874d8e05eef2..4c55c0c84be239e12c5b2e9cbe73bbd6f0cbd74e 100644 GIT binary patch delta 50 zcmdlabVq1|5vNuPLl#3KLmopqLq3BpLlHwNLkdGCLotIb5SlXRF&G1}0Rt}s7Xtt% C%?Shm delta 7 Ocmca3v`J`#5hnl)wgR32 diff --git a/stude/config/settings.py b/stude/config/settings.py index b3f4c1a..a4dfed3 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -261,11 +261,30 @@ LEAFLET_CONFIG = { 'TILES': 'https://openstreetmap.keannu1.duckdns.org/tile/{z}/{x}/{y}.png' } +REDIS_HOST = os.getenv('REDIS_HOST', 'localhost') +REDIS_PORT = os.getenv('REDIS_PORT', 6379) + + +# Django Redis Cache +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://redis:6379/1", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + } + } +} + +SESSION_ENGINE = "django.contrib.sessions.backends.cache" +SESSION_CACHE_ALIAS = "default" + +# Redis Cache for Django Channel Websockets CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { - "hosts": [("127.0.0.1", 6379)], + "hosts": [("redis", 6379)], }, }, } diff --git a/stude/courses/views.py b/stude/courses/views.py index 3e596e5..f4659ef 100644 --- a/stude/courses/views.py +++ b/stude/courses/views.py @@ -1,3 +1,5 @@ +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page from rest_framework import generics from .models import Course from .serializers import CourseSerializer @@ -6,3 +8,7 @@ from .serializers import CourseSerializer class CourseListView(generics.ListAPIView): serializer_class = CourseSerializer queryset = Course.objects.all() + + @method_decorator(cache_page(60*60)) + def dispatch(self, *args, **kwargs): + return super().dispatch(*args, **kwargs) diff --git a/stude/wait_for_redis.py b/stude/wait_for_redis.py index ecec825..0b68f4e 100644 --- a/stude/wait_for_redis.py +++ b/stude/wait_for_redis.py @@ -4,7 +4,6 @@ import redis REDIS_HOST = os.getenv('REDIS_HOST', 'localhost') REDIS_PORT = os.getenv('REDIS_PORT', 6379) - if __name__ == '__main__': while True: try: