From 8f14299e4c7ada1193880c3f4bc929161059e35d Mon Sep 17 00:00:00 2001 From: Bae KwonHan Date: Tue, 1 Oct 2024 14:28:26 +0900 Subject: [PATCH] add cache for session list and retrieve API --- pyconkr/settings.py | 10 ++++++++++ session/viewsets.py | 30 +++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pyconkr/settings.py b/pyconkr/settings.py index 92481f0..26b722e 100644 --- a/pyconkr/settings.py +++ b/pyconkr/settings.py @@ -219,6 +219,16 @@ "PREPROCESSING_HOOKS": ["pyconkr.openapi.preprocessing_filter_spec"], } + +# cache for django localmem +# https://docs.djangoproject.com/en/5.1/topics/cache/#local-memory-caching +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "pyconkr-api-v2", + } +} + # CORS_ALLOW_ALL_ORIGINS = True CORS_ORIGIN_WHITELIST = ( "https://2023.pycon.kr", diff --git a/session/viewsets.py b/session/viewsets.py index 9ef57c4..b806d5a 100644 --- a/session/viewsets.py +++ b/session/viewsets.py @@ -1,4 +1,6 @@ from django.conf import settings +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page from drf_spectacular.utils import OpenApiExample, OpenApiResponse, extend_schema from rest_framework.permissions import IsAuthenticatedOrReadOnly from rest_framework.response import Response @@ -28,14 +30,24 @@ def get_queryset(self): 200: OpenApiResponse( response=str, examples=[ - OpenApiExample(name="2023년 세션 목록", value=SessionListSerializer(many=True)), - OpenApiExample(name="2024년 이후 세션 목록 (Pretalx)", value=PretalxSessionSerializer(many=True)), + OpenApiExample( + name="2023년 세션 목록", value=SessionListSerializer(many=True) + ), + OpenApiExample( + name="2024년 이후 세션 목록 (Pretalx)", + value=PretalxSessionSerializer(many=True), + ), ], ), }, ) + # cache list about 30 minutes + @method_decorator(cache_page(60 * 30)) def list(self, request, *args, **kwargs) -> Response: - if request.version == 2023 or request.version not in settings.PRETALX.EVENT_NAME: + if ( + request.version == 2023 + or request.version not in settings.PRETALX.EVENT_NAME + ): return super().list(request, *args, **kwargs) pretalx_event_name = settings.PRETALX.EVENT_NAME[request.version] @@ -52,13 +64,21 @@ def list(self, request, *args, **kwargs) -> Response: response=str, examples=[ OpenApiExample(name="2023년 세션 상세", value=SessionSerializer()), - OpenApiExample(name="2024년 이후 세션 상세 (Pretalx)", value=PretalxSessionSerializer()), + OpenApiExample( + name="2024년 이후 세션 상세 (Pretalx)", + value=PretalxSessionSerializer(), + ), ], ), }, ) + # cache each about 30 minutes + @method_decorator(cache_page(60 * 30)) def retrieve(self, request, *args, **kwargs) -> Response: - if request.version == 2023 or request.version not in settings.PRETALX.EVENT_NAME: + if ( + request.version == 2023 + or request.version not in settings.PRETALX.EVENT_NAME + ): return super().retrieve(request, *args, **kwargs) pretalx_event_name = settings.PRETALX.EVENT_NAME[request.version]