现在我正在使用 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/