gpt4 book ai didi

data-structures - Redis Hyperloglog - PFCOUNT 副作用

转载 作者:IT王子 更新时间:2023-10-29 06:01:38 26 4
gpt4 key购买 nike

Redis 最近发布了名为 HyperLogLog 的新数据结构。它允许我们保留唯一对象的数量,并且只占用 12k 字节的大小。我不明白的是Redis的PFCOUNT命令在技术上说是写命令。为什么会这样?

Note: as a side effect of calling this function, it is possible that the HyperLogLog is modified, since the last 8 bytes encode the latest computed cardinality for caching purposes. So PFCOUNT is technically a write command.

最佳答案

HyperLogLog对象的头部如下:

struct hllhdr {
char magic[4]; /* "HYLL" */
uint8_t encoding; /* HLL_DENSE or HLL_SPARSE. */
uint8_t notused[3]; /* Reserved for future use, must be zero. */
uint8_t card[8]; /* Cached cardinality, little endian. */
uint8_t registers[]; /* Data bytes. */
};

注意 card 字段:它包含算法评估的最后一个基数。计算基数的估计值是一项昂贵的操作,因此 Redis 会缓存该值并将其保存在该字段中。

当 PFADD 被调用时,HyperLogLog 对象可能会被更新,也可能不会被更新(很有可能不会)。如果未更新,调用 PFCOUNT 将重用缓存值(卡字段)。如果更新了,则card字段失效,所以下一个PFCOUNT会执行计数算法,将新值写入card字段。

这就是 PFCOUNT 可以改变 HyperLogLog 对象的原因。

关于data-structures - Redis Hyperloglog - PFCOUNT 副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164374/

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