From a1532835eacfdfaa252af7a813e028891eaa2efb Mon Sep 17 00:00:00 2001 From: Vasily Zubarev Date: Mon, 27 Jan 2020 14:38:45 +0100 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9Clast=5Fmodified=5Fat=E2=80=9D=20?= =?UTF-8?q?caching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- boards/cache.py | 5 +++++ boards/views.py | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 boards/cache.py diff --git a/boards/cache.py b/boards/cache.py new file mode 100644 index 0000000..c5c2962 --- /dev/null +++ b/boards/cache.py @@ -0,0 +1,5 @@ +from boards.models import Board + + +def board_last_modified_at(request, board_slug): + return Board.objects.filter(slug=board_slug).first().refreshed_at diff --git a/boards/views.py b/boards/views.py index 48d3c36..d577453 100644 --- a/boards/views.py +++ b/boards/views.py @@ -4,8 +4,10 @@ from django.conf import settings from django.core.cache import cache from django.shortcuts import render, get_object_or_404 from django.views.decorators.cache import cache_page +from django.views.decorators.http import last_modified from auth.helpers import authorized_user +from boards.cache import board_last_modified_at from boards.models import Board, BoardBlock, BoardFeed @@ -17,6 +19,7 @@ def index(request): }) +@last_modified(board_last_modified_at) def board(request, board_slug): board = get_object_or_404(Board, slug=board_slug) @@ -28,7 +31,8 @@ def board(request, board_slug): }, status=401) cached_page = cache.get(f"board_{board.slug}") - if cached_page and board.refreshed_at <= datetime.utcnow() - timedelta(seconds=settings.BOARD_CACHE_SECONDS): + if cached_page and board.refreshed_at <= \ + datetime.utcnow() - timedelta(seconds=settings.BOARD_CACHE_SECONDS): return cached_page blocks = BoardBlock.objects.filter(board=board) @@ -59,4 +63,3 @@ def what(request): @cache_page(settings.STATIC_PAGE_CACHE_SECONDS) def privacy_policy(request): return render(request, "docs/privacy_policy.html") -