#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070185c4dfefa2-2cdd58da-5dee-4984-9df3-7018ec0ceabf-000000%40eu-central-1.amazonses.com.

Reply via email to