ConsistentHash
in package
For a thorough description of consistent hashing, see http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/, and also the original paper: http://www8.org/w8-papers/2a-webserver/caching/paper2.html
Copyright 2007-2017 Horde LLC (http://www.horde.org/)
Tags
Table of Contents
- $_circle : array<string|int, mixed>
- Array representing our circle
- $_nodeCount : int
- Number of nodes
- $_nodes : array<string|int, mixed>
- Array of nodes.
- $_numberOfReplicas : int
- Number of times to put each node into the hash circle per weight value.
- $_pointCount : int
- Number of points on the circle
- $_pointMap : array<string|int, mixed>
- Numeric indices into the circle by hash position
- __construct() : mixed
- Create a new consistent hash, with initial $nodes at $numberOfReplicas
- add() : mixed
- Add $node with weight $weight
- addNodes() : mixed
- Add multiple nodes to the hash with the same weight.
- get() : string
- Get the primary node for $key.
- getNodes() : array<string|int, mixed>
- Get an ordered list of nodes for $key.
- hash() : string
- Expose the hash function for testing, probing, and extension.
- remove() : mixed
- Remove $node from the hash.
- _updateCircle() : mixed
- Maintain the circle and arrays of points.
Properties
$_circle
Array representing our circle
protected
array<string|int, mixed>
$_circle
= []
$_nodeCount
Number of nodes
protected
int
$_nodeCount
= 0
$_nodes
Array of nodes.
protected
array<string|int, mixed>
$_nodes
= []
$_numberOfReplicas
Number of times to put each node into the hash circle per weight value.
protected
int
$_numberOfReplicas
= 100
$_pointCount
Number of points on the circle
protected
int
$_pointCount
= 0
$_pointMap
Numeric indices into the circle by hash position
protected
array<string|int, mixed>
$_pointMap
= []
Methods
__construct()
Create a new consistent hash, with initial $nodes at $numberOfReplicas
public
__construct([array<string|int, mixed> $nodes = [] ][, int $weight = 1 ][, int $numberOfReplicas = 100 ]) : mixed
Parameters
- $nodes : array<string|int, mixed> = []
-
Initial array of nodes to add at $weight.
- $weight : int = 1
-
The weight for the initial node list.
- $numberOfReplicas : int = 100
-
The number of points on the circle to generate for each node.
Return values
mixed —add()
Add $node with weight $weight
public
add(mixed $node[, mixed $weight = 1 ]) : mixed
Parameters
- $node : mixed
- $weight : mixed = 1
Return values
mixed —addNodes()
Add multiple nodes to the hash with the same weight.
public
addNodes(array<string|int, mixed> $nodes[, int $weight = 1 ]) : mixed
Parameters
- $nodes : array<string|int, mixed>
-
An array of nodes.
- $weight : int = 1
-
The weight to add the nodes with.
Return values
mixed —get()
Get the primary node for $key.
public
get(string $key) : string
Parameters
- $key : string
-
The key to look up.
Return values
string —The primary node for $key.
getNodes()
Get an ordered list of nodes for $key.
public
getNodes(string $key[, int $count = 5 ]) : array<string|int, mixed>
Parameters
- $key : string
-
The key to look up.
- $count : int = 5
-
The number of nodes to look up.
Return values
array<string|int, mixed> —An ordered array of nodes.
hash()
Expose the hash function for testing, probing, and extension.
public
hash(string $key) : string
Parameters
- $key : string
Return values
string —Hash value
remove()
Remove $node from the hash.
public
remove(mixed $node) : mixed
Parameters
- $node : mixed
Return values
mixed —_updateCircle()
Maintain the circle and arrays of points.
protected
_updateCircle() : mixed