6

I am using the django-redis backend and the django.core.cache.cache module. The django cache module does not seem to support proper functionality of pushing to lists and manipulating certain data structures.

The implied implementation used to update a list in the django cache module:

my_list = cache.get('my_list')
my_list.append('my value')

cache.set('my_list', my_list)

This approach is not efficient because the entire list is being loaded into the application server's memory.

Redis has support for the LPUSH / RPUSH commands to dynamically update a list. However, it doesn't look like these methods are available in the django cache module.

The official python redis client seems to implement these methods. Is there any reason why django wouldn't offer this implementation? I'm asking out of my curiosity. Possibly I missed some details?

Robert Christopher
  • 461
  • 1
  • 6
  • 15

1 Answers1

6

It does support raw client and command access, for that you would have to get access to raw client instead of using django cache.

https://github.com/jazzband/django-redis#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. To avoid storing another setting for creating a raw connection, django-redis exposes functions with which you can obtain a raw client reusing the cache connection string: get_redis_connection(alias).

Code example:

>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
>>> con
<redis.client.StrictRedis object at 0x2dc4510>
>>> con.lpush('mylist',1)
Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • But it does not support to store object data (such as list or dict) into the list. You should use `json.dumps` before pushing the data and `json.loads` when you pop it. Or you can use [lock](https://niwinz.github.io/django-redis/latest/#_locks) to meet your function. – ramwin Apr 10 '18 at 09:51
  • Curious. Does the raw client still do the prefixing and versioning done by the Django cache interface? – DylanYoung May 10 '19 at 14:01