I am working on a Django app where I want to use redis for cache purpose.
I see that there are few packages like django-redis and django-redis-cache which integrates with Django and one can use to access redis.
But, with those packages you only have 'get' and 'set' commands to use.
How to use other redis commands for lists,sets, sorted sets like rpush, lrange, zadd?
Can we use it with the above mentioned packages (django-redis, django-redis-cache) or we need to use redis-py client?
Thanks for your help!
You can access the raw redis connection in django-redis. I believe this allows you to execute commands via redis-py which it uses under the hood.
Using of Raw client access :
In some situations your application requires access to a raw Redis client to use some advanced features that aren’t exposed by the Django cache interface.
>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
Now we can execute raw commands of Redis data type :
Example :
Storing data into Redis hash.
Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects.
# Create framework dictionary in python
>>> frameworks = {'python':'Django','php':'Laravel','java':'Spring'}
#Store them into redis hash.
>>> con.hmset('frameworks',frameworks)
True #successfully stored
# retrieved number of items
>>> con.hlen('frameworks')
3
#Get all values
>>> con.hvals('frameworks')
[b'Django', b'Laravel', b'Spring']
Hash commands used in above example :
hmset : Set multiple items
hlen : Get number of items
hvals : return all values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With