[Django] #35039: Redis cache.clear() should respect KEY_PREFIX

94 views
Skip to first unread message

Django

unread,
Dec 14, 2023, 5:49:49 PM12/14/23
to django-...@googlegroups.com
#35039: Redis cache.clear() should respect KEY_PREFIX
-------------------------------------+-------------------------------------
Reporter: Peter | Owner: nobody
Baumgartner |
Type: Bug | Status: new
Component: Core | Version: 4.2
(Cache system) |
Severity: Normal | Keywords: cache redis
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

[https://stackoverflow.com/questions/21975228/redis-python-how-to-delete-
all-keys-according-to-a-specific-pattern-in-python/45153525#45153525 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

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

Django

unread,
Dec 14, 2023, 11:41:09 PM12/14/23
to django-...@googlegroups.com
#35039: Redis cache.clear() should respect KEY_PREFIX
-------------------------------------+-------------------------------------
Reporter: Peter Baumgartner | Owner: nobody
Type: New feature | Status: closed
Component: Core (Cache system) | Version: 4.2
Severity: Normal | Resolution: wontfix

Keywords: cache redis | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Bug => New feature
* resolution: => wontfix


Comment:

This behavior is consistent with other backends and is
[https://docs.djangoproject.com/en/stable/topics/cache/#django.core.cache.cache.clear
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.

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

Reply all
Reply to author
Forward
0 new messages