#34271 closed Uncategorized (duplicate)

Add a simple page caching method

Reported by: Niccolò Mineo Owned by: nobody
Component: Core (Cache system) Version: 4.1
Severity: Normal Keywords:
Cc: Ahter Sönmez Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)
    )

Change History (1)

comment:1 by Carlton Gibson, 22 months ago

Cc: Ahter Sönmez added
Resolution: duplicate
Status: newclosed

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!

Note: See TracTickets for help on using tickets.
Back to Top