[Django] #34271: Add a simple page caching method

20 views
Skip to first unread message

Django

unread,
Jan 18, 2023, 7:34:51 AM1/18/23
to django-...@googlegroups.com
#34271: Add a simple page caching method
-----------------------------------------------+------------------------
Reporter: Niccolò Mineo | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (Cache system) | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
Since the current `cache_page` implementation caches according to the
client and it is quite limiting in that sense, I'd like to propose a
companion to such method to cache according to the path only. Something
along these lines:

{{{
from pathlib import Path

from django.conf import settings
from django.core.cache import caches

default_cache = caches[settings.CACHE_MIDDLEWARE_ALIAS]
cache_key = ".".join(Path(request.path[1:]).parts)

if not (response := default_cache.get(cache_key)):
response = view_func(request, *args, **kwargs)
response.add_post_render_callback(
lambda r: default_cache.set(cache_key, r, timeout)
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34271>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2023, 7:56:53 AM1/18/23
to django-...@googlegroups.com
#34271: Add a simple page caching method
-------------------------------------+-------------------------------------

Reporter: Niccolò Mineo | Owner: nobody
Type: Uncategorized | Status: closed

Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* cc: Ahter Sönmez (added)
* status: new => closed
* resolution: => duplicate


Comment:

I think this is related to #5815 - per-view cache invalidation.

The issue there is it's ''Not Easy™'' to generate the right cache key
outside of the request context.

9 time 10 I've wanted to just clear by the path as suggested here.

Can you think about whether we can make the make key function more
pluggable and so solve #5815 in a way compatible with your desire here?

I'm going to provisionally mark this as a duplicate. If on investigation
that's not right, do re-open and I'll take a further look.
Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/34271#comment:1>

Reply all
Reply to author
Forward
0 new messages