[Django] #33604: Allow `CacheMiddleware.key_prefix` to be a callable

8 views
Skip to first unread message

Django

unread,
Mar 28, 2022, 12:39:27 PM3/28/22
to django-...@googlegroups.com
#33604: Allow `CacheMiddleware.key_prefix` to be a callable
------------------------------------------------+------------------------
Reporter: Alexandru Mărășteanu | Owner: nobody
Type: New feature | Status: new
Component: Core (Cache system) | Version: dev
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 |
------------------------------------------------+------------------------
As discussed in https://groups.google.com/g/django-
developers/c/UbD1DkV1uPo and https://code.djangoproject.com/ticket/11269
it would be useful to be able to specify a callable which would allow for
more dynamic caching. In my case, I need to cache differently depending on
whether the user is authenticated or not. Another scenario is to cache
certain pages depending on some timestamp. Other uses cases were mentioned
in the linked pages.

There exists https://github.com/peterbe/django-fancy-cache but I was able
to address this by extending the built-in middleware so I really don't see
an argument for a 3rd party package.

I'll try to open a PR for this.

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

Django

unread,
Mar 28, 2022, 1:01:29 PM3/28/22
to django-...@googlegroups.com
#33604: Allow `CacheMiddleware.key_prefix` to be a callable
-------------------------------------+-------------------------------------
Reporter: Alexandru | Owner: nobody
Mărășteanu |

Type: New feature | Status: new
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Thanks for the ticket. Dynamic prefixes are not supported, however you can
use the [https://docs.djangoproject.com/en/stable/topics/cache/#cache-key-
transformation KEY_FUNCTION] cache setting and changed `key_prefix`
dynamically. I'm not convinced that we really need another option here.

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

Django

unread,
Mar 28, 2022, 1:53:42 PM3/28/22
to django-...@googlegroups.com
#33604: Allow `CacheMiddleware.key_prefix` to be a callable
-------------------------------------+-------------------------------------
Reporter: Alexandru | Owner: nobody
Mărășteanu |
Type: New feature | Status: new
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Alexandru Mărășteanu):

Replying to [comment:1 Mariusz Felisiak]:


> Thanks for the ticket. Dynamic prefixes are not supported, however you

can use the [https://docs.djangoproject.com/en/stable/topics/cache/#cache-
key-transformation KEY_FUNCTION] cache setting and changed `key_prefix`


dynamically. I'm not convinced that we really need another option here.

The need is to cache dynamically depending on the current request, on
request properties like the user, or the query params etc. One of my use
cases is to cache pages for anonymous users only. Another one is to cache
pages based on some value stored in Redis. Another thing I want to do is
to force it to skip the cache based on a query param (appending a random
value is not always possible). Currently I do this with a custom
middleware which extends the builtin one and simply updates
`self.key_prefix` at the appropriate times. It seems to me it's a small
change with great utility.

The `KEY_FUNCTION` would be useful here if it received the request. But
allowing a callable `key_prefix` is a lot cheaper than changing the
signature of `KEY_FUNCTION`.

--
Ticket URL: <https://code.djangoproject.com/ticket/33604#comment:2>

Reply all
Reply to author
Forward
0 new messages