redisdjango-redis

django-redis and redis returning different results


I am using django-redis in my server to make less queries to the db. I recently setup a lambda where I needed to access the same data from the redis cache. But it seems that I am served different data if I connect to the redis cache using django-redis and if I use the python library redis.

The behaviour could be reproduced using the following code. import redis from redis.connection import HiredisParser from redis.connection import ConnectionPool

connection_pool = ConnectionPool.from_url(url = 'redis://localhost:6379/1', parser_class=HiredisParser)

redis_client = redis.Redis(connection_pool=connection_pool)
redis_client.set('my_key', 'my_value')

# Access Redis data
redis_value = redis_client.get('my_key')
print(redis_value)

The response is b'my_value

from django_redis.cache import RedisCache

django_redis = RedisCache(server='redis://localhost:6379/1', params = {'OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient'}})
print(django_redis.get('my_key'))

The response is None

I also tried creating the connection without a pool but it also didnt work.


Solution

  • django redis adds a prefix to the keys when creating them. The prefix is supposed to be the version of the data stored. The default is version 1. So if you dont provide a version and your key is my_key then you key will be changed to :1:my_key