mirror of
https://github.com/predis/predis.git
synced 2026-08-30 04:02:22 +00:00
Change how the hash of a node is retrieved in the hashring-based distributor.
Now classes that extend Predis\Distribution\HashRing can override how the hash of a node is retrieved in order to implement different kind of distribution strategies while reusing the base algorithm.
This commit is contained in:
@@ -80,7 +80,7 @@ class HashRing implements IDistributionStrategy {
|
||||
|
||||
protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
|
||||
$nodeObject = $node['object'];
|
||||
$nodeHash = (string) $nodeObject;
|
||||
$nodeHash = $this->getNodeHash($nodeObject);
|
||||
$replicas = (int) round($weightRatio * $totalNodes * $replicas);
|
||||
for ($i = 0; $i < $replicas; $i++) {
|
||||
$key = crc32("$nodeHash:$i");
|
||||
@@ -88,6 +88,10 @@ class HashRing implements IDistributionStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
protected function getNodeHash($nodeObject) {
|
||||
return (string) $nodeObject;
|
||||
}
|
||||
|
||||
public function generateKey($value) {
|
||||
return crc32($value);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class KetamaPureRing extends HashRing {
|
||||
|
||||
protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
|
||||
$nodeObject = $node['object'];
|
||||
$nodeHash = (string) $nodeObject;
|
||||
$nodeHash = $this->getNodeHash($nodeObject);
|
||||
$replicas = (int) floor($weightRatio * $totalNodes * ($replicas / 4));
|
||||
for ($i = 0; $i < $replicas; $i++) {
|
||||
$unpackedDigest = unpack('V4', md5("$nodeHash-$i", true));
|
||||
|
||||
Reference in New Issue
Block a user