razorpay/php-bloom-filter

此包已被废弃,不再维护。未建议替代包。

检查对象存在性的最佳方式,同时节省内存和磁盘使用。

0.8.0 2016-04-29 08:52 UTC

This package is auto-updated.

Last update: 2022-08-23 23:48:54 UTC


README

Build Status

布隆过滤器 - 是检查对象存在性的最佳方式,同时节省内存和磁盘使用。

使用方法

示例和基准测试文件在 examples 目录中可用。

基本参数
  • entries_max (int) 对象的最大条目数。默认:100。
  • error_chance (float) (0;1) 检查存在性时出错的机会。用于自动设置下一个2个参数。默认:0.001。
  • set_size (int) 集合对象的大小。默认:计算得出。
  • hash_count (int) 唯一哈希对象的数量。默认:计算得出。
  • counter (boolean) 如果您还想删除元素,则使用此参数。会减慢速度。默认:false。
  • hash (array) 哈希对象的参数。
  • strtolower (boolean) 是否将字符串转换为小写。默认:true;
简单代码示例
use Razorpay\BloomFilter\Bloom;

$parameters = array(
  'entries_max' => 2
);
$bloom = new Bloom($parameters);

/**
* Set value to Bloom filter
* You can put single string or array of infinite deep of strings
*
* @param mixed
* @return BloomObject
*/
$bloom->set('Some string');
echo $bloom->has('Some string'); //true
echo $bloom->has('Some string', false); //0

/**
* Test set with given array or string, to check it existance
* You can put single string or array of infinite deep of strings
*
* @param mixed (array) or (string)
* @param boolean return boolean or float number of tests
* @return mixed (array) or (boolean) or (float)
*/
echo $bloom->has('Other string'); //false

/**
* Unset value from Bloom filter
* You can put single string or array of infinite deep of strings
* Only works with counter parameter
* @param mixed
* @return mixed (boolean) or (array)
*/
$bloom->delete('Some string');
echo $bloom->has('Some string'); //false
缓存

您还可以通过使用 serialize() 和 unserialize() 函数来缓存整个对象。

何时使用
  • Get/Set 操作超过 0.001
  • 您需要一个快速检查大集合,超过 100,000 个条目
用例
  • 减少 HDD 使用率
  • 加速对象存在性的检查(带有计数器 40% 慢,但仍然比本地快得多)
  • 节省内存
许可证

BSD-3-Clause