Skip to content

KEY_PREFIX is ignored when using get_redis_connection() from django-redis #785

@vahid-fathi

Description

@vahid-fathi

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:

  1. 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",
    }
  1. Use cache:
from django.core.cache import cache
cache.set("test_key", "value")
  1. Stored key in Redis: myapp:test_key which is correct
  2. 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")
  1. 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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions