Opened 4 months ago

Closed 4 months ago

#35465 closed New feature (wontfix)

Feature: allow pre-caching and disable-cache

Reported by: mdk Owned by: nobody
Component: Core (Cache system) Version: 5.0
Severity: Normal Keywords: cache
Cc: mdk Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by mdk)

TL;DR: If there was a way to inhibit cache.get() (making it to always return default), it would enable to use cases :

  • One could implement a pre-caching tool like a simple curl in a crontab.
  • One could implement a "disable cache" feature like the one in browsers network panel, but applied server-side.

Longer explanation:

My initial idea is was to prefill the cache (say template fragments) using a cron to guarantee cache hits for humans.

In the current Django, I can't do it: the cron would often just cache hit, doing nothing, and a random human will cache miss and have a slower request.

So I think I need a way to tell "if it's the pre-caching cron, don't try to 'get' from the cache, just compute everything slowly, and fill the cache with fresh data'.

And for this to work I « just need » to replace the real get during the pre-caching step with:

    def get(self, key, default=None, version=None):
        return default

For example, if I have a 15mn cache with a 10mn cron, the cache will never be close to expire, and it would be impossible for a user to cache miss.

Once this implemented, it could be "reused"/"recycled" as a "disable-cache" feature: say for example the Django Debug Toolbar could have a checkbox "disable cache", it would have the strange side effect to populate the cache, but it would work as expected: the presented data would be fresh.

Say we're going this way, it would need for a way to tell "this request is without cache", like a cookie, a query string parameter, or whatever fits in an HTTP request. If there's no concensus on how to flag this "cache disabled" behavior it could be implemented as a dotted path to a function returning a boolean, as SHOW_TOOLBAR_CALLBACK from Django debug toolbar.

There's a "security" catch to be aware of: if it's too easy to disable cache on production servers, like ?cache=no it could be used to DoS, it probably has to be a tad more secure like by using a secret.

I have not tried to implement it, I'd like to discuss it before, so: does it looks like a good idea? Has it been discussed many times before that I missed maybe?

Change History (3)

comment:1 by mdk, 4 months ago

Description: modified (diff)

comment:2 by Sarah Boyce, 4 months ago

Hello!
As this is a new feature request, the recommended path forward is to first propose the idea on the Django Forum and see what the community thinks about the suggestion. You'll reach a much wider audience and likely get extra feedback.
I'll close the ticket for now, but if there is a community agreement for this, you are welcome to come back to the ticket and point to the forum topic, so we can then re-open it.
For more details, please see the documented guidelines for requesting features.

comment:3 by Sarah Boyce, 4 months ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top