-
-
Notifications
You must be signed in to change notification settings - Fork 441
Description
Describe the bug
Hi,
There appears to be an inconsistency in how the KEY_PREFIX setting is applied in django-redis.
When using the standard Django cache interface (django.core.cache.cache), the KEY_PREFIX works correctly and is applied to all keys. However, when using get_redis_connection() from django_redis, the prefix is not applied, even if it's defined in the cache settings.
To Reproduce
Steps to reproduce the behavior:
- Configure Django’s cache with django-redis:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
"KEY_PREFIX": "myapp",
}
- Use cache:
from django.core.cache import cache
cache.set("test_key", "value")
- Stored key in Redis:
myapp:test_keywhich is correct - but when use
get_redis_connection:
from django_redis import get_redis_connection
redis_client = get_redis_connection()
redis_client.set("test_key_2", "value_2")
- Stored key in Redis:
test_key_2(expected:myapp:test_key_2)
Expected behavior
Even when using get_redis_connection(), there should be an option or wrapper to respect the configured KEY_PREFIX, or at least this behavior should be clearly documented to avoid confusion.
Let me know if this is intended, or if you would accept a PR to improve this behavior or clarify it in the docs.
Thanks!
Environment:
- Python version: [3.12.3]
- Django Redis Version: [6.0.0]
- Django Version: [e.g. 5.2.4]
- Redis Version: [Redis server v=8.0.3]
- redis-py Version: [5.2.1]