Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache for session list and retrieve API #171

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyconkr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 25 additions & 5 deletions session/viewsets.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
Loading