palicao/php-rebloom

0.2.1 2020-10-27 20:40 UTC

This package is auto-updated.

Last update: 2024-09-27 03:37:09 UTC


README

使用PHP与Redis Bloom!

Code Climate maintainability Code Climate coverage Build Status Latest Stable Version PHP version GitHub

免责声明:这是一个非常轻量级的库。要获得完整的体验,请查看:https://github.com/averias/phpredis-bloom

安装

composer require palicao/php-rebloom

布隆过滤器

布隆过滤器是一种空间高效的概率数据结构,用于确定一个元素是否在集合中。可能会出现误报。

$bloomFilter = new BloomFilter(
    new RedisClient(
        new Redis(),
        new RedisConnectionParams($host, $port)
    )
);

预留

BloomFilter::reserve(string $key, float $error, int $capacity): bool

创建一个具有给定期望错误比率和初始容量的空布隆过滤器。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfreserve

插入

BloomFilter::insert(string $key, string $value, ?float $error = null, ?int $capacity = null): bool

将一个项目添加到布隆过滤器中,如果过滤器尚不存在,则创建它。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfaddhttps://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert

插入多个

BloomFilter::insertMany(string $key, array $values, ?float $error = null, ?int $capacity = null): bool[]

将多个项目添加到BloomFilter中,如果过滤器尚不存在,则创建它。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfmaddhttps://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert

如果键存在则插入

BloomFilter::insertIfKeyExists(string $key, string $value): bool

将一个项目添加到布隆过滤器中,只有当过滤器已经存在时。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfaddhttps://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert

如果键存在则插入多个

BloomFilter::insertManyIfKeyExists(string $key, array $values): bool[]

将多个项目添加到布隆过滤器中,只有当过滤器已经存在时。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfaddhttps://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert

存在

BloomFilter::exists(string $key, string $value): bool

检查项目是否存在。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfadd

多个存在

BloomFilter::manyExist(string $key, array $values): bool[]

检查多个项目是否存在。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfmexists

scanDumploadChunkscopy

BloomFilter::scanDump(string $key): array BloomFilter::loadChunks(string $key, array $chunks): void BloomFilter::copy(string $sourceKey, string $destKey): void

scanDump将整个布隆过滤器导出到一个数组中,可以通过loadChunks分块加载。使用前两个函数的copy函数,可以快速将一个布隆过滤器复制到不同的键。

https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfscandumphttps://oss.redislabs.com/redisbloom/Bloom_Commands/#bfloadchunk

库克鸟过滤器

布谷鸟过滤器与布隆过滤器类似,但它的空间效率更高,并允许删除项。

$cuckooFilter = new CuckooFilter(
    new RedisClient(
        new Redis(),
        new RedisConnectionParams($host, $port)
    )
);

预留

CuckooFilter::reserve(string $key, int $capacity, ?int $bucketSize = null, ?int $maxIterations = null, ?int $expansion = null): bool

创建一个具有初始容量的空布谷鸟过滤器。误报率固定在约3%,取决于过滤器有多满。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfreserve

插入

CuckooFilter::insert(string $key, string $value, bool $allowDuplicateValues = true, ?int $capacity = null): bool

将项添加到布谷鸟过滤器中,如果不存在则创建过滤器。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddnxhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinserthttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinsertnx

插入多个

CuckooFilter::insertMany(string $key, array $values, bool $allowDuplicateValues = true, ?int $capacity = null): bool[]

类似于上一个,将多个值添加到键中。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddnxhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinserthttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinsertnx

如果键存在则插入

CuckooFilter::insertIfKeyExists(string $key, string $value, bool $allowDuplicateValues = true, ?int $capacity = null): bool

如果项存在,则在布谷鸟过滤器中插入项。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddnxhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinserthttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinsertnx

如果键存在则插入多个

CuckooFilter::insertManyIfKeyExists(string $key, array $values, bool $allowDuplicateValues = true, ?int $capacity = null): bool[]

如果项存在,则在布谷鸟过滤器中插入多个项。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfaddnxhttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinserthttps://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfinsertnx

存在

CuckooFilter::exists(string $key, string $value): bool

如果布谷鸟过滤器存在,则返回true。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfexists

delete

CuckooFilter::delete(string $key, string $value): bool

在过滤器中删除一个项。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfdel

count

CuckooFilter::count(string $key, string $value): int

返回一个项可能在过滤器中出现的次数。

参见 https://oss.redislabs.com/redisbloom/Cuckoo_Commands/#cfcount

scanDumploadChunkscopy

CuckooFilter::scanDump(string $key): array CuckooFilter::loadChunks(string $key, array $chunks): void CuckooFilter::copy(string $sourceKey, string $destKey): void

scanDump 以数组的形式导出整个布谷鸟过滤器,可以通过 loadChunks 分块加载。使用前两个函数的 copy 函数,可以快速将一个布谷鸟过滤器复制到不同的键。

CountMinSketch

Count-Min Sketch 是一种概率数据结构,用作数据流中事件频率表。

$countMinSketch = new CountMinSketch(
    new RedisClient(
        new Redis(),
        new RedisConnectionParams($host, $port)
    )
);

initByDimensions

CountMinSketch::initByDimensions(string $key, int $width, int $depth): bool

使用用户提供的 $width$depth 初始化名为 $key 的 CountMinSketch。

参见 https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsinitbydim

initByProbability

CountMinSketch::initByProbability(string $key, float $error, float $probability): bool

初始化 CountMinSketch 以适应所需的错误率和膨胀计数概率。

参见 https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsinitbyprob

incrementBy

CountMinSketch::incrementBy(string $key, Pair ...$pairs): bool

在 CountMinSketch 中按给定值增加一个或多个项。一个 Pair 表示一个项和我们想要增加的值。

参见 https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsincrby

query

CountMinSketch::query(string $key, string ... $items): Pair[]

返回一个或多个项的近似计数。

请参阅https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsquery

merge

将多个CountMinSketch合并成一个,使得每个项目的值是每个sketch中值的总和。$sourceKeysWeightMap是一个关联数组,其中每个键是一个sketch,值是权重,即我们想要每个项目计数乘以的值,在合并之前。

CountMinSketch::merge(string $destinationKey, array $sourceKeysWeightMap) : bool

请参阅https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsmerge

info

返回一个CountMinSketchInfo实例,其中包含有关宽度、深度和sketch总计数的信息。

CountMinSketch::info(string $key): CountMinSketchInfo

请参阅https://oss.redislabs.com/redisbloom/CountMinSketch_Commands/#cmsinfo

TopK

与CountMinSketch类似,基于以下算法描述:https://www.usenix.org/conference/atc18/presentation/gong

预留

TopK::reserve(string $key, int $topK, int $width, int $depth, float $decay): bool

为计算$topK个最高元素预保留一个TopK,给定$width和$depth,并指定$decay(在已占用的桶中减少计数器的概率)。

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkreserve

add

将一个或多个项目添加到TooK中。如果一个项目进入Top-K列表,则被驱逐的项目将返回在添加的项目占用的位置。

TopK::add(string $key, string ... $items): array

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkadd

incrementBy

通过增量增加数据结构中一个项目的得分。类似于add,被驱逐的项目将返回。

TopK::incrementBy(string $key, Pair ...$pairs): array

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkincrby

query

返回包含在Top-K中的元素的$items子集。

TopK::query(string $key, string ... $items): string[]

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkquery

count

返回包含在top-k中的元素的$items子集,以及它们的近似计数。

TopK::count(string $key, string ... $items): Pair[]

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkcount

list

返回具有相对位置的top-k项目。

TopK::list(string $key): Pair[]

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topklist

info

返回包含有关大小、宽度、深度和衰减的Top-KInfo对象。

TopK::info(string $key): TopKInfo

请参阅https://oss.redislabs.com/redisbloom/TopK_Commands/#topkinfo