An open API service indexing awesome lists of open source software.

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)

Awesome Lists containing this project

README

          

HashRing [![Build Status](https://travis-ci.org/chxj1992/hashring.svg?branch=master)](https://travis-ci.org/chxj1992/hashring)
============================

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](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");
```