dev-master
9999999-dev https://ninth.not-bad.orgPHPX Consistent Hash Library
MIT License
The Requires
- php >=5.3.3
The Development Requires
by Psyduck.Mans
cache library memcached hash open source extenstion consistent hash phpx
Wallogit.com
2017 © Pedro Peláez
PHPX Consistent Hash Library
PHPX/ConsistentHash is a PHP library which implements [http://en.wikipedia.org/wiki/Consistent_hashing consistent hashing], which is most useful in distributed caching. The implement of \PHPX\ConsistentHash\Impl\Flexihash refer to Flexihash(https://github.com/pda/flexihash), (*1)
<?php
use PHPX\ConsistentHash\Hasher\Flexihash\Crc32Hasher;
use PHPX\ConsistentHash\Impl\Flexihash;
use PHPX\ConsistentHash\TargetUnit;
use PHPX\ConsistentHash\Target\HostTarget;
$hashSpace = new Flexihash(new Crc32Hasher());
// bulk add
$targetUnits = new \ArrayIterator(array(
new TargetUnit(new HostTarget('redis-a', 6379), 1),
new TargetUnit(new HostTarget('redis-b', 6379), 3),
new TargetUnit(new HostTarget('redis-c', 6379), 2.5)
));
$hashSpace->addTargets($targetUnits);
// simple lookup
$hashSpace->lookup('object-a'); // "redis-a"
$hashSpace->lookup('object-b'); // "redis-b"
// add and remove
$weight = 20.3;
$hashSpace
->addTarget(new TargetUnit(new HostTarget('redis-d', 6379), $weight))
->removeTarget(new HostTarget('redis-a', 6379));
// lookup with next-best fallback (for redundant writes)
$hashSpace->lookupList('object', 2); // ["redis-b", "redis-d"]
// remove redis-b, expect object to hash to redis-d
$hashSpace->removeTarget(new HostTarget('redis-b', 6379));
$hashSpace->lookup('object'); // "redis-d"
PHPX Consistent Hash Library
MIT License
cache library memcached hash open source extenstion consistent hash phpx