Skip to content

Commit

Permalink
Merge pull request #171 from darjeeling/devdev
Browse files Browse the repository at this point in the history
add cache for session list and retrieve API
  • Loading branch information
darjeeling authored Oct 1, 2024
2 parents 28a5491 + 8f14299 commit e50f099
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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

0 comments on commit e50f099

Please sign in to comment.