使用 python redis 时,redis 流长度限制不起作用
the redis stream length limit not work when using 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 参数没有生效?
默认情况下,redis-py
中 xadd
的 approximate
参数设置为 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 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 参数没有生效?
默认情况下,redis-py
中 xadd
的 approximate
参数设置为 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