[Django]-Django and redis adding :1 to keys

3👍

:1 it’s version

cache.set(key, value, timeout=DEFAULT_TIMEOUT, version=None)

You can remove it by set empty string:

cache.set("foo", "bar",version='')

In the redis you will get:

::foo

0👍

According to the Django Documentation on Cache Key transformation:

"The cache key provided by a user is not used verbatim – it is combined with the cache prefix and key version to provide a final cache key. By default, the three parts are joined using colons to produce a final string:"

def make_key(key, key_prefix, version):
    return "%s:%s:%s" % (key_prefix, version, key)

The version defaults to 1 and the key-prefix defaults to an empty string. Therefore, your resulting key looks like :1:my_key.

In case you want to change the version and key-prefix, you can do this in your CACHES settings as shown below:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://xxxxx/0",
        "VERSION": "my_version",
        "KEY_PREFIX": "my_prefix",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
       }
   }
}

In that case, the code cache.set(my_key, my_value, 3600) would create the key my_prefix:my_version:my_key. The cache settings for VERSION and KEY_PREFIX would then also allow you to get the required value by using the key "my_key", so cache.get(my_key) will get you the correct value.

Leave a comment