Add server cache
This commit is contained in:
parent
d435837c80
commit
edbc01122d
@ -2,6 +2,7 @@ from datetime import datetime, timedelta, date
|
||||
|
||||
from django.db.models import F, Q, Value
|
||||
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_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework import viewsets
|
||||
@ -14,11 +15,12 @@ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, Calen
|
||||
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='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Agency.objects.all()
|
||||
serializer_class = AgencySerializer
|
||||
@ -26,8 +28,8 @@ class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=[LAST_MODIFIED_CACHE])
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class StopViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Stop.objects.all()
|
||||
serializer_class = StopSerializer
|
||||
@ -35,8 +37,8 @@ class StopViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class RouteViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Route.objects.all()
|
||||
serializer_class = RouteSerializer
|
||||
@ -44,8 +46,8 @@ class RouteViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class TripViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Trip.objects.all()
|
||||
serializer_class = TripSerializer
|
||||
@ -53,8 +55,8 @@ class TripViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = StopTime.objects.order_by('id').all()
|
||||
serializer_class = StopTimeSerializer
|
||||
@ -64,8 +66,8 @@ class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
ordering = ['stop_sequence', ]
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Calendar.objects.all()
|
||||
serializer_class = CalendarSerializer
|
||||
@ -73,8 +75,8 @@ class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = CalendarDate.objects.all()
|
||||
serializer_class = CalendarDateSerializer
|
||||
@ -82,16 +84,16 @@ class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
filterset_fields = '__all__'
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class TransferViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Transfer.objects.all()
|
||||
serializer_class = TransferSerializer
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
|
||||
|
||||
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||
@method_decorator(name='list', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
@method_decorator(name='retrieve', decorator=[CACHE_CONTROL, LAST_MODIFIED])
|
||||
class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = FeedInfo.objects.all()
|
||||
serializer_class = FeedInfoSerializer
|
||||
|
@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from corsheaders.defaults import default_headers
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
@ -61,6 +63,12 @@ CORS_ALLOWED_ORIGINS = [
|
||||
"http://localhost:3000",
|
||||
]
|
||||
|
||||
CORS_ALLOW_HEADERS = (
|
||||
*default_headers,
|
||||
"If-Modified-Since",
|
||||
'Cache-Control',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = "sncf.urls"
|
||||
|
||||
TEMPLATES = [
|
||||
|
Loading…
Reference in New Issue
Block a user