pythondjangocachingredisdjango-redis

Django redis LPUSH / RPUSH


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?


Solution

  • 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)