1
Fork 0

Add content cache

This commit is contained in:
Jake Howard 2024-04-15 21:12:55 +01:00
parent 325fec5271
commit 3a32e32fca
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 9 additions and 0 deletions

View file

@ -3,6 +3,8 @@ from django.http import HttpResponse
from django.db import connections from django.db import connections
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.template import Context from django.template import Context
from django.views.decorators.cache import cache_page
def search(request): def search(request):
with connections["default"].cursor() as cursor: with connections["default"].cursor() as cursor:
@ -19,3 +21,5 @@ def content(request, slug):
"page": page, "page": page,
"content": page.content_template.render(Context({"request": request, "page": page})) "content": page.content_template.render(Context({"request": request, "page": page}))
}) })
cached_content = cache_page(600, cache="mem")(content)

View file

@ -90,6 +90,10 @@ CACHES = {
"default": { "default": {
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache", "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
"LOCATION": BASE_DIR / "cache", "LOCATION": BASE_DIR / "cache",
},
"mem": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "mem",
} }
} }

View file

@ -21,4 +21,5 @@ urlpatterns = [
path("search/", views.search), path("search/", views.search),
path("content/<slug:slug>/", views.content), path("content/<slug:slug>/", views.content),
path("__debug__/", include("debug_toolbar.urls")), path("__debug__/", include("debug_toolbar.urls")),
path("cached-content/<slug:slug>/", views.cached_content)
] ]