flexihash/flexihash

Flexihash是一个小的PHP库,实现了一致性哈希,这在分布式缓存中非常有用。它需要PHP5,并使用PHPUnit进行单元测试。

v3.0.0 2020-08-07 00:58 UTC

This package is not auto-updated.

Last update: 2024-09-20 04:15:28 UTC


README

Build Status Coverage Status

Flexihash是一个小的PHP库,实现了一致性哈希,这在分布式缓存中非常有用。它需要PHP5,并使用PHPUnit进行单元测试。

安装

Composer是推荐的安装方法。您可以在Packagist上找到flexihash,因此安装非常简单

composer require flexihash/flexihash

或在你的composer.json

{
    "require": {
        "flexihash/flexihash": "^3.0.0"
    }
}

使用

$hash = new Flexihash();

// bulk add
$hash->addTargets(['cache-1', 'cache-2', 'cache-3']);

// simple lookup
$hash->lookup('object-a'); // "cache-1"
$hash->lookup('object-b'); // "cache-2"

// add and remove
$hash
  ->addTarget('cache-4')
  ->removeTarget('cache-1');

// lookup with next-best fallback (for redundant writes)
$hash->lookupList('object', 2); // ["cache-2", "cache-4"]

// remove cache-2, expect object to hash to cache-4
$hash->removeTarget('cache-2');
$hash->lookup('object'); // "cache-4"

测试

单元测试

% vendor/bin/phpunit

基准测试

% vendor/bin/phpunit tests/BenchmarkTest.php

进一步阅读