[Django]-Why django redis cache cannot get the data in redis

4👍

The Django cache adds a prefix to cache keys. By default, this depends on KEY_PREFIX and VERSION in your CACHES settings. You can also customise the behaviour by using a custom KEY_FUNCTION.

You can use the make_key method to find out the full cache key:

>>> from django.core.cache import cache
>>> cache.make_key('somekey')
':1:somekey'

You can use this full key when you call redis_conn.set().

As you pointed out in the comments, there is a second difficulty. Django-redis serializes the cache values. By default it uses Python pickle but there is also a JSON serializer available or you can choose your own.

When you write to the cache using redis_conn.set(), you will have to serialize the data in the same way so that django-redis can read it.

Leave a comment