Search code examples
djangoredisdjango-redis

Is normal django_redis generate this kind of session key?


I'm new to django_redis lib. I'm using this confs for session store with 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": ""
    }
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
SESSION_CACHE_ALIAS = "default"
...

Everything seems to work properly. But, when i check the key for a session on the database (default sqlite) and then compare that key value with the redis db in redis-cli, the session key are diferent. In the redis-cli version the session key has a prefix, even when I set no prefix.

DB (sqlite) Version of Session Key

skxn0oqp3goeipt6hnwvpeyp83hhoao0

redis-cli version of the key

127.0.0.1:6379[1]> keys *
1) ":1:django.contrib.sessions.cached_dbskxn0oqp3goeipt6hnwvpeyp83hhoao0"
127.0.0.1:6379[1]> 

Is this normal ?


Solution

  • The value in the session table is, by definition, a session key, so it doesn't need to record any additional information.

    The value in the cache sits alongside every other value in the cache, not just other session keys, so it has to be namespaced appropriately. In this case, it records:

    • Which Django server this is (the KEY_PREFIX, which you're not using)
    • The version number of this cached value (1)
    • The module that is using the value (django.contrib.sessions.cached_db)
    • Finally, the actual session key (skxn0oqp3goeipt6hnwvpeyp83hhoao0)

    The last two are specified by the session backend itself. That key, the version, and the prefix are combined by make_key(), which can be overridden by the cache backend.

    So yes, this is normal.