From 3a32e32fca64eddab2b777d69734decad8b81495 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 15 Apr 2024 21:12:55 +0100 Subject: [PATCH] Add content cache --- yamdl_playground/core/views.py | 4 ++++ yamdl_playground/settings.py | 4 ++++ yamdl_playground/urls.py | 1 + 3 files changed, 9 insertions(+) diff --git a/yamdl_playground/core/views.py b/yamdl_playground/core/views.py index ee513ee..2790566 100644 --- a/yamdl_playground/core/views.py +++ b/yamdl_playground/core/views.py @@ -3,6 +3,8 @@ from django.http import HttpResponse from django.db import connections from django.shortcuts import get_object_or_404, render from django.template import Context +from django.views.decorators.cache import cache_page + def search(request): with connections["default"].cursor() as cursor: @@ -19,3 +21,5 @@ def content(request, slug): "page": page, "content": page.content_template.render(Context({"request": request, "page": page})) }) + +cached_content = cache_page(600, cache="mem")(content) diff --git a/yamdl_playground/settings.py b/yamdl_playground/settings.py index 141e7ff..c21fed2 100644 --- a/yamdl_playground/settings.py +++ b/yamdl_playground/settings.py @@ -90,6 +90,10 @@ CACHES = { "default": { "BACKEND": "django.core.cache.backends.filebased.FileBasedCache", "LOCATION": BASE_DIR / "cache", + }, + "mem": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "mem", } } diff --git a/yamdl_playground/urls.py b/yamdl_playground/urls.py index 5808ffa..93f54fb 100644 --- a/yamdl_playground/urls.py +++ b/yamdl_playground/urls.py @@ -21,4 +21,5 @@ urlpatterns = [ path("search/", views.search), path("content//", views.content), path("__debug__/", include("debug_toolbar.urls")), + path("cached-content//", views.cached_content) ]