myphps/flexhash

Flexihash是一个实现一致性哈希的PHP库,在分布式缓存中非常有用。

v1.1 2022-10-11 10:10 UTC

This package is auto-updated.

Last update: 2024-09-27 05:35:20 UTC


README

Build Status Coverage Status

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

安装

Composer是推荐的安装方法。您可以在Packagist上找到flexhash,安装非常简单,只需

composer require myphps/flexihash

或在您的composer.json文件中

{
    "require": {
        "myphps/flexhash": "^1.0"
    }
}

用法

$hash = new FlexHash();

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

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

// add and remove
$hash
  ->addNode('cache-4')
  ->removeNode('cache-1');

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

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

测试

单元测试

% vendor/bin/phpunit

基准测试

% vendor/bin/phpunit tests/BenchmarkTest.php

进一步阅读