From 7cd88c9fbd3196c72bba9b731fb792968157b24d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 4 Sep 2022 17:29:04 +0100 Subject: [PATCH] Just set cache control rather than caching page content This avoids storing tonnes of stuff in the cache, when it doesn't matter _too_ much --- website/contrib/code_block/views.py | 4 ++-- website/legacy/views.py | 6 +++--- website/urls.py | 12 ++++++++++-- website/well_known/views.py | 8 ++++---- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/website/contrib/code_block/views.py b/website/contrib/code_block/views.py index ffa20f4..d45e3e5 100644 --- a/website/contrib/code_block/views.py +++ b/website/contrib/code_block/views.py @@ -1,10 +1,10 @@ from django.http import HttpRequest, HttpResponse from django.utils.datastructures import OrderedSet -from django.views.decorators.cache import cache_page +from django.views.decorators.cache import cache_control from pygments.formatters.html import HtmlFormatter -@cache_page(3600) +@cache_control(max_age=3600) def pygments_styles(request: HttpRequest) -> HttpResponse: default_styles = ( HtmlFormatter(style="default") diff --git a/website/legacy/views.py b/website/legacy/views.py index fd06e54..7ebcc82 100644 --- a/website/legacy/views.py +++ b/website/legacy/views.py @@ -1,18 +1,18 @@ from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator -from django.views.decorators.cache import cache_page +from django.views.decorators.cache import cache_control from django.views.generic import RedirectView from website.blog.models import BlogPostListPage -@method_decorator(cache_page(60 * 60), name="dispatch") +@method_decorator(cache_control(max_age=60 * 60), name="dispatch") class PostsFeedView(RedirectView): def get_redirect_url(self) -> str: post_list = get_object_or_404(BlogPostListPage) return post_list.url + post_list.reverse_subpage("feed") -@method_decorator(cache_page(60 * 60), name="dispatch") +@method_decorator(cache_control(max_age=60 * 60), name="dispatch") class AllPagesFeedView(RedirectView): pattern_name = "feed" diff --git a/website/urls.py b/website/urls.py index fc5e68f..84c7027 100644 --- a/website/urls.py +++ b/website/urls.py @@ -29,8 +29,16 @@ urlpatterns = [ name="wagtailimages_serve", ), path("sitemap.xml", cache_page(60 * 60)(sitemap), name="sitemap"), - path("robots.txt", cache_page(60 * 60)(RobotsView.as_view()), name="robotstxt"), - path("keybase.txt", cache_page(60 * 60)(KeybaseView.as_view()), name="keybase"), + path( + "robots.txt", + cache_control(max_age=60 * 60)(RobotsView.as_view()), + name="robotstxt", + ), + path( + "keybase.txt", + cache_control(max_age=60 * 60)(KeybaseView.as_view()), + name="keybase", + ), path("404/", page_not_found, name="404"), path("feed/", AllPagesFeed(), name="feed"), path("", include("website.legacy.urls")), diff --git a/website/well_known/views.py b/website/well_known/views.py index 710b5dd..a573cfb 100644 --- a/website/well_known/views.py +++ b/website/well_known/views.py @@ -4,7 +4,7 @@ from django.http.request import HttpRequest from django.http.response import HttpResponse from django.utils import timezone from django.utils.decorators import method_decorator -from django.views.decorators.cache import cache_page +from django.views.decorators.cache import cache_control from django.views.generic import TemplateView from website.contact.models import ContactPage @@ -17,7 +17,7 @@ class SecurityView(TemplateView): expires = timedelta(days=7) - @method_decorator(cache_page(int(expires.total_seconds() / 2))) + @method_decorator(cache_control(max_age=int(expires.total_seconds() / 2))) def dispatch(self, request: HttpRequest) -> HttpResponse: return super().dispatch(request) @@ -36,13 +36,13 @@ class SecurityView(TemplateView): return context -@method_decorator(cache_page(60 * 60), name="dispatch") +@method_decorator(cache_control(max_age=60 * 60), name="dispatch") class MatrixServerView(TemplateView): template_name = "well-known/matrix-server.json" content_type = "application/json" -@method_decorator(cache_page(60 * 60), name="dispatch") +@method_decorator(cache_control(max_age=60 * 60), name="dispatch") class MatrixClientView(TemplateView): template_name = "well-known/matrix-client.json" content_type = "application/json"