#35039 closed New feature (wontfix)

Redis cache.clear() should respect KEY_PREFIX

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

Description

The Redis cache backend clear method calls the Redis command flushdb. This makes sense when KEY_PREFIX is not defined and the user has access to the full keyspace.

Current Behavior

If a user has defined a KEY_PREFIX, it is likely they don't have access to the full keyspace or don't want the Redis cache changing keys outside the KEY_PREFIX. In these scenarios, the user either gets a permissions error calling cache.clear() or deletes keys that are outside what the Django cache manages.

Expected Behavior

When KEY_PREFIX is defined, cache.clear() only deletes keys with that prefix.

Possible solutions

This StackOverflow answer benchmarks a few different ways to delete all keys with a specific prefix.

django-redis has a delete_pattern method https://github.com/jazzband/django-redis#scan--delete-keys-in-bulk

Change History (1)

comment:1 by Mariusz Felisiak, 11 months ago

Resolution: wontfix
Status: newclosed
Type: BugNew feature

This behavior is consistent with other backends and is documented so it's not a bug:

"Be careful with this; clear() will remove everything from the cache, not just the keys set by your application"

We agreed in #33012 to keep the builtin Redis backend minimal and consistent with other backends. It looks like you should use django-redis for your use case.

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