https://github.com/chxj1992/hashring
Consistent hashing "hashring" implementation in php (using the same algorithm as libketama)
https://github.com/chxj1992/hashring
consistent-hashing hashring libketama php
Last synced: 28 days ago
JSON representation
Consistent hashing "hashring" implementation in php (using the same algorithm as libketama)
- Host: GitHub
- URL: https://github.com/chxj1992/hashring
- Owner: chxj1992
- License: wtfpl
- Created: 2017-02-19T14:41:50.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-03-10T12:57:00.000Z (almost 4 years ago)
- Last Synced: 2025-11-27T14:49:06.655Z (about 1 month ago)
- Topics: consistent-hashing, hashring, libketama, php
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
HashRing [](https://travis-ci.org/chxj1992/hashring)
============================
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](https://github.com/serialx/hashring).
Using
============================
Install ::
```bash
composer require chxj1992/hashring:~1.0
```
Basic example usage ::
```php
$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 ::
```php
$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 ::
```php
$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");
```