makinacorpus/php-bloom

Bloom过滤器实现

2.0.2 2017-09-19 10:50 UTC

This package is auto-updated.

Last update: 2024-09-10 21:18:56 UTC


README

Build Status

这是一个简单的PHP Bloom过滤器实现,使用了Sherif Ramadan的实现。

原始代码和非常好的解释可以在以下链接找到 http://phpden.info/Bloom-filters-in-PHP

对原始代码进行了轻微修改,以纠正一些编码规范问题,实现更灵活的运行时配置,并修复了一些性能问题。

用法

您首先必须选择一个目标最大元素数,这是您的过滤器将包含的,以及一个误报实现,显然这两个数字越小,实现的速度越快。

// You may cache this value, and fetch it back, it's the whole goal of this
// API. Beware that the stored string might contain ``\0`` characters, ensure
// your storage API deals with those strings in safe way.
$value = null;

// Configure your Bloom filter, if you store the value, you should store the
// configuration along since selected hash algorithms and string size would
// change otherwise.
$probability = 0.0001
$maxSize = 10000;

$filter = new \MakinaCorpus\Bloom\BloomFilter();

// You may add as many elements as you wish, elements can be any type, really,
// if not scalar they will be serialized prior to being hashed.
$filter->set('some_string');
$filter->set(123456);
$filter->set(['some' => 'array']);
$filter->set(new \stdClass());

// And the whole goal of it:
if ($filter->check('some_value')) {
  do_something();
}

注意事项

请仔细阅读原始作者的博客文章,因为它解释了您需要了解的关于Bloom过滤器的所有内容:http://phpden.info/Bloom-filters-in-PHP

请明智地使用它,散列算法相当快,但如果您过度使用,将对CPU使用产生负面影响。

还有许多其他具有竞争力的实现,您可以选择其中最适合您的,选择之前请多看看。