3

I'm using django-redis to store some data on my website, and I have a problem where Redis is adding :1 at the beginning so my key looks like this: :1:my_key

I'm not sure why is it doing this, I've read the documentation on django-redis and I couldn't find anything related, so my guess it has something to do with redis but I can't figure out what.

In my settings.py I have the regular:

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

And in my tasks.py I set keys like the documentation says:

from django.core.cache import cache
cache.set(my_key, my_value, 3600)

So now I can't get the values using the cache.get(my_key)

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.