This time for you to bring PHP implementation of the consistency hash algorithm implementation steps, PHP implementation of the consistency hash algorithm implementation of the notes are what, the following is the actual case, together to see.
<?php//+----------------------------------------------------------------------//| Perfect is shit//+----------------------------------------------------------------------//| PHP implementation: Consistent hash algorithm//+----------------------------------------------------------------------//| Author:alexander <gt199899@gmail.com>//+----------------------------------------------------------------- -----// | datetime:2017-01-11 16:01:36//+----------------------------------------------------------------------//| Copyright:perfect is shit//+----------------------------------------------------------------------class consistenthashing{//ring//Hash node Private $_ring = Array (); All nodes//nodes, hash public $nodes = Array (); The virtual node of each node public $virtual = 64; /** * construct * @param array $nodes initialized node list */Public function construct ($nodes = Array ()) {if (!empty ($nodes)) { foreach ($nodes as $value) {$this->addnode ($value); }}}/** * Get the ring content * @Return array $this->_ring */Public Function getring () {return $this->_ring; }/** * time33 function * @param string $STR * @return 32-bit positive integer * @author great Gods */Public Function time33 ($STR) {// hash (i) = hash (i-1) * + str[i]//$hash = 5381; # # will hash set to 0, unexpectedly than set to 5381 better distribution effect!!! $hash = 0; $s = MD5 ($STR); Compared to other versions, the MD5 encryption $seed = 5; $len = 32;//Encrypted length of 5 for ($i = 0; $i < $len; $i + +) {//(hash <<) + hash equals hash *//$hash = SP rintf ("%u", $hash *) + ord ($s {$i}); $hash = ($hash * + ord ($s {$i}) & 0x7FFFFFFF; $hash = ($hash << $seed) + $hash + ord ($s {$i}); } return $hash & 0x7FFFFFFF; /** * Add node * @param string $node node name * @return Object $this */Public Function AddNode ($node) {if (In_ar Ray ($node, Array_keys ($this->nodes)) {return; } for ($i = 1; $i <= $this->virtual; $i + +) {$key = $this->time33 ($node. '-' . $i); $this->_ring[$key] = $node; $this->nodes[$node] [] = $key; } ksort ($this->_ring, sort_numeric); return $this; }/** * Gets the hash of the string mapped to the node above the ring * @param string $key * @return String $node */Public Function GetNode ($key) { $node = current ($this->_ring); $hash = $this->time33 ($key); foreach ($this->_ring as $key = + $value) {if ($hash <= $key) {$node = $value; Break }} return $node; }/** * Gets the key mapped to a specific node * This method needs to be called manually, non-special cases are not recommended to use this method in the program * @param string $node * @param string $keyPre * @return Mixed */Public Function GetKey ($node, $keyPre = "") {if (!in_array ($node, Array_keys ($this->nodes))) {return fals E } $result = false; for ($i =1; $i <=10000; $i + +) {$key = $keyPre. MD5 (RAND (1000, 9999)); if ($this->getnode ($key) = = = $node) {$result = true; Break }} return $result? $key: false; }} $ch _obj = new consistenthashing (); $ch _obj->addnode (' Node_1$ch _obj->addnode (' node_2 '); $ch _obj->addnode (' node_3 '); $ch _obj->addnode (' node_4 '); $ch _obj-> AddNode (' node_5 '); $ch _obj->addnode (' node_6 ');//+----------------------------------------------------------- -----------// | View the node to which the key is mapped//+----------------------------------------------------------------------$key 1 = " Asofiwjamfdalksjfkasasdflasfja "; $key 2 =" JAKSLDFJLASFJSDJFIOAFASLKJFLSADKJFL "; $key 3 =" Asjldflkjasfsdjflkajkldsjfksajdlflajs "; $key 4 =" Iowanfasijfmasdnfoas "; $key 5 =" Pqkisndfhoalnfiewlkl "; $key 6 =" Qjklasjdifoajfalsjflsa "; Echo sprintf ("%-50s Maps to Node%s\n ", $key 1, $ch _obj->getnode ($key 1)); Echo sprintf ("%-50s Maps to Node %s\n ", $key 2, $ch _obj->getnode ($key 2)), Echo sprintf ("%-50s Maps to Node%s\n ", $key 3, $ch _obj->getnode ($key 3)); Echo sprintf ("%-50s Maps to Node%s\n", $key 4, $ch _obj->getnode ($key 4)), Echo sprintf ("%-50s Maps to Node%s\n", $key 5, $ch _obj-> GetNode ($key 5)); Echo sprintf ("%-50s Maps to Node%s\n", $key 6, $ch _obj->getnode ($key 6));//+---------------------------- ------------------------------------------// | View Ring and node information//+----------------------------------------------------------------------//var_dump ($ch _obj-> Getring ());//Var_dump ($ch _obj->nodes);//+------------------------------------------------------------------ ----// | Gets the key//of a specific node +----------------------------------------------------------------------//$key 1 = $ch _obj->getkey ( ' Node_1 ', ' pre_ ');//Var_dump ($key 1);//+----------------------------------------------------------------------// | Test distribution//+----------------------------------------------------------------------//$keys = Array ();//$rings = Array () ;//For ($i = 1; $i <= 60000; $i + +) {//$key = SHA1 (rand (1000000,9999999));//$node = $ch _obj->getnode ($key);// $rings [$node] = Isset ($rings [$node])? + + $rings [$node]: 1;//}//var_dump ($rings);
Operation Result:
Asofiwjamfdalksjfkasasdflasfja Map to node NODE_1JAKSLDFJLASFJSDJFIOAFASLKJFLSADKJFL map to Node Node_ 2asjldflkjasfsdjflkajkldsjfksajdlflajs Map to node Node_1iowanfasijfmasdnfoas map to Node Node_2pqkisndfhoalnfiewlkl Map to Node Node_3qjklasjdifoajfalsjflsa map to Node node_5
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP RSA ciphertext encryption and decryption steps detailed
PHP Local API Interface test steps in detail