chxj1992/hashring

Consistent hashing "hashring" implementation in php (using the same algorithm as libketama)

Maintainers

Package info

github.com/chxj1992/hashring

Type:project

pkg:composer/chxj1992/hashring

Statistics

Installs: 71 769

Dependents: 0

Suggesters: 0

Stars: 6

Open Issues: 1

v1.0.1 2017-02-19 16:17 UTC

This package is not auto-updated.

Last update: 2026-03-12 07:06:15 UTC


README

WTFPL

Implements consistent hashing that can be used when the number of server nodes can increase or decrease (like in memcached). The hashing ring is built using the same algorithm as libketama.

Inspired by a golang hashring library serialx/hashring.

Using

Install ::

composer require chxj1992/hashring:~1.0

Basic example usage ::

$memcacheServers = ["192.168.0.246:11212",
                    "192.168.0.247:11212",
                    "192.168.0.249:11212"];

$hashRing = new \Chxj1992\HashRing\HashRing($memcacheServers);
$server = $ring->getNode("my_key");

Using weights example ::

$weights = ["192.168.0.246:11212" => 1,
            "192.168.0.247:11212" => 2,
            "192.168.0.249:11212" => 1];

$hashRing = new \Chxj1992\HashRing\HashRing($weights);
$server = $hashRing->getNode("my_key");

Adding and removing nodes example ::

$memcacheServers = ["192.168.0.246:11212",
                    "192.168.0.247:11212",
                    "192.168.0.249:11212"];

$hashRing = new \Chxj1992\HashRing\HashRing($memcacheServers);
$hashRing = $hashRing->removeNode("192.168.0.246:11212");
$hashRing = $hashRing->addNode("192.168.0.250:11212");
$server = $hashRing->getNode("my_key");