Opened 3 years ago

Last modified 6 months ago

#33604 assigned New feature

Allow CacheMiddleware.key_prefix to be a callable.

Reported by: Alexandru Mărășteanu Owned by: Stefan Farmbauer
Component: Core (Cache system) Version: dev
Severity: Normal Keywords:
Cc: Tobias Kunze Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Stefan Farmbauer)

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.

(PR is 18254)https://github.com/django/django/pull/18254

Change History (13)

comment:1 by Mariusz Felisiak, 3 years ago

Thanks for the ticket. Dynamic prefixes are not supported, however you can use the KEY_FUNCTION cache setting and changed key_prefix dynamically. I'm not convinced that we really need another option here.

in reply to:  1 comment:2 by Alexandru Mărășteanu, 3 years ago

Replying to Mariusz Felisiak:

Thanks for the ticket. Dynamic prefixes are not supported, however you can use the 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.

comment:3 by Mariusz Felisiak, 3 years ago

Summary: Allow `CacheMiddleware.key_prefix` to be a callableAllow CacheMiddleware.key_prefix to be a callable.
Triage Stage: UnreviewedAccepted

Tentatively accepted.

comment:4 by Mariusz Felisiak, 3 years ago

Cc: Tobias Kunze added

in reply to:  3 comment:5 by Alexandru Mărășteanu, 3 years ago

Replying to Mariusz Felisiak:

Tentatively accepted.

I started writing some tests the other evening so I'll try to open a PR later today.

comment:6 by Alexandru Mărășteanu, 3 years ago

Has patch: set
Owner: changed from nobody to Alexandru Mărășteanu
Status: newassigned

I opened at PR at https://github.com/django/django/pull/15553 Unfortunately the tests stop running at some point due to an error which I've yet to figure out.

Version 0, edited 3 years ago by Alexandru Mărășteanu (next)

comment:7 by Mariusz Felisiak, 3 years ago

Needs documentation: set
Patch needs improvement: set

Docs changes are missing. Also, many tests don't work.

in reply to:  7 ; comment:8 by Alexandru Mărășteanu, 3 years ago

Replying to Mariusz Felisiak:

Docs changes are missing. Also, many tests don't work.

I fixed the tests and updated the docs

comment:9 by Mariusz Felisiak, 3 years ago

Needs tests: set

in reply to:  8 comment:10 by noneNote, 22 months ago

Last edited 22 months ago by noneNote (previous) (diff)

comment:11 by Stefan Farmbauer, 6 months ago

Owner: changed from Alexandru Mărășteanu to Stefan Farmbauer

comment:12 by Stefan Farmbauer, 6 months ago

Owner: changed from Stefan Farmbauer to Alexandru Mărășteanu

comment:13 by Stefan Farmbauer, 6 months ago

Description: modified (diff)
Owner: changed from Alexandru Mărășteanu to Stefan Farmbauer
Note: See TracTickets for help on using tickets.
Back to Top