Add server cache

This commit is contained in:
Emmy D'Anello 2024-02-02 00:04:40 +01:00
parent d435837c80
commit edbc01122d
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 29 additions and 19 deletions

View File

@ -2,6 +2,7 @@ from datetime import datetime, timedelta, date
from django.db.models import F, Q, Value from django.db.models import F, Q, Value
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control
from django.views.decorators.http import last_modified from django.views.decorators.http import last_modified
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets from rest_framework import viewsets
@ -14,11 +15,12 @@ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, Calen
Transfer, FeedInfo Transfer, FeedInfo
LAST_MODIFIED_CACHE = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version)) CACHE_CONTROL = cache_control(max_age=7200)
LAST_MODIFIED = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version))
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class AgencyViewSet(viewsets.ReadOnlyModelViewSet): class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Agency.objects.all() queryset = Agency.objects.all()
serializer_class = AgencySerializer serializer_class = AgencySerializer
@ -26,8 +28,8 @@ class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=[LAST_MODIFIED_CACHE]) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class StopViewSet(viewsets.ReadOnlyModelViewSet): class StopViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Stop.objects.all() queryset = Stop.objects.all()
serializer_class = StopSerializer serializer_class = StopSerializer
@ -35,8 +37,8 @@ class StopViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class RouteViewSet(viewsets.ReadOnlyModelViewSet): class RouteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Route.objects.all() queryset = Route.objects.all()
serializer_class = RouteSerializer serializer_class = RouteSerializer
@ -44,8 +46,8 @@ class RouteViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class TripViewSet(viewsets.ReadOnlyModelViewSet): class TripViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Trip.objects.all() queryset = Trip.objects.all()
serializer_class = TripSerializer serializer_class = TripSerializer
@ -53,8 +55,8 @@ class TripViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class StopTimeViewSet(viewsets.ReadOnlyModelViewSet): class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = StopTime.objects.order_by('id').all() queryset = StopTime.objects.order_by('id').all()
serializer_class = StopTimeSerializer serializer_class = StopTimeSerializer
@ -64,8 +66,8 @@ class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
ordering = ['stop_sequence', ] ordering = ['stop_sequence', ]
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class CalendarViewSet(viewsets.ReadOnlyModelViewSet): class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Calendar.objects.all() queryset = Calendar.objects.all()
serializer_class = CalendarSerializer serializer_class = CalendarSerializer
@ -73,8 +75,8 @@ class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet): class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
queryset = CalendarDate.objects.all() queryset = CalendarDate.objects.all()
serializer_class = CalendarDateSerializer serializer_class = CalendarDateSerializer
@ -82,16 +84,16 @@ class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class TransferViewSet(viewsets.ReadOnlyModelViewSet): class TransferViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Transfer.objects.all() queryset = Transfer.objects.all()
serializer_class = TransferSerializer serializer_class = TransferSerializer
filter_backends = [DjangoFilterBackend] filter_backends = [DjangoFilterBackend]
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE) @method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet): class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
queryset = FeedInfo.objects.all() queryset = FeedInfo.objects.all()
serializer_class = FeedInfoSerializer serializer_class = FeedInfoSerializer

View File

@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
from pathlib import Path from pathlib import Path
from corsheaders.defaults import default_headers
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
@ -61,6 +63,12 @@ CORS_ALLOWED_ORIGINS = [
"http://localhost:3000", "http://localhost:3000",
] ]
CORS_ALLOW_HEADERS = (
*default_headers,
"If-Modified-Since",
'Cache-Control',
)
ROOT_URLCONF = "sncf.urls" ROOT_URLCONF = "sncf.urls"
TEMPLATES = [ TEMPLATES = [