python - 使用python redis时redis流长度限制不起作用

标签 python redis

现在我正在使用 python( Python 3 ) redis 客户端将元素添加到 redis 流中,这是 lib 的依赖项:

redis~=5.0.3
然后这是我使用 Python 将元素写入 redis 的代码:
def push_message_to_stream(article):
    try:
        message = {
            "id": article.id,
            "sub_source_id": article.sub_source_id
        }
        #
        # Redis did not remove the ack message automatic
        # when the element is full, it remove the oldest
        #
        redis_client.xadd(name=article_stream_name, fields=message, maxlen=10)
        # redis_client.xgroup_create(name=article_stream_name, groupname=article_group_name)
        # redis_client.publish(channel=article_stream_name, message="hello world!")
    except Exception as e:
        logger.error("write stream error:", e)
该元素可以成功写入redis,但是在我添加maxlen的参数之后到 10,当我检查 redis 中的流元素时,它仍然有超过 90 多个元素在流中,我猜测 redis 可能使用旧的创建者配置,然后我尝试删除流并重新创建它,但仍然在这种情况下,其中我做错了吗?为什么 maxlen 参数没有任何作用?

最佳答案

默认情况下 approximate xadd 的参数在 redis-py设置为 True .这意味着您不会获得精确的长度修剪,并为您提供更好的性能。如果您想要确切的长度,请尝试:

redis_client.xadd(name=article_stream_name, fields=message, maxlen=10, approximate=False)
xadd 的源代码:https://redis-py.readthedocs.io/en/stable/_modules/redis/client.html#Redis.xadd

关于python - 使用python redis时redis流长度限制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67479940/

相关文章:

python - Tensorflow 中的基本添加?

node.js - Redis实例中如何高效保存商品价格图数据

java - 有没有办法在 Redis 和关系数据库中使用带有 @RedisHash 的实体?

python - 类型错误 : <lambda>() missing 1 required positional argument: 'w'

python - torch.empty() 中 "out"参数的奇怪行为

php - Laravel 所有 session ID 与 Redis 驱动程序

具有数据大小的 HMSET、HGETALL 的 Redis 基准测试

node.js - req.session 未定义?

python - 如何修改Python字典值并创建字典列表?

python - pygame - 将鼠标对齐到网格