gpt4 book ai didi

python - Django redis LPUSH/RPUSH

转载 作者:IT王子 更新时间:2023-10-29 05:59:19 25 4
gpt4 key购买 nike

我正在使用 django-redis 后端和 django.core.cache.cache 模块。django 缓存模块似乎不支持推送到列表和操作某些数据结构的正确功能。

用于更新 django 缓存模块中列表的隐含实现:

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

cache.set('my_list', my_list)

这种方法效率不高,因为整个列表都被加载到应用程序服务器的内存中。

Redis 支持 LPUSH/RPUSH 命令来动态更新列表。但是,看起来这些方法在 django 缓存模块中不可用。

官方的python redis客户端好像实现了这些方法。django 有什么理由不提供这个实现吗?我是出于好奇才问的。可能我错过了一些细节?

最佳答案

它确实支持原始客户端和命令访问,因为您必须访问原始客户端而不是使用 django 缓存。

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

代码示例:

>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
>>> con
<redis.client.StrictRedis object at 0x2dc4510>
>>> con.lpush('mylist',1)

关于python - Django redis LPUSH/RPUSH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com