An example of a consistent hash distribution algorithm

Source: Internet
Author: User

This article describes the implementation of a consistent hash algorithm using PHP.

Create a Flexihash class that has two member variables and three methods:

$serverlist: Save the list of servers.

$isSorted: Records whether the server list has been sequenced.

Addserver: Add a server to the server list;

Removeserver: Remove a server from the server list;

Lookup: Find the right server to store data in the current server.

"Example":

<span style= "Font-family:microsoft yahei;font-size:18px;"       ><?              Class flexihash{Private $serverList = Array ();                           Private $isSorted =false;                     Public Functionaddserver ($server) {$hash =mhash ($server);                     if (!isset ($this-serverlist[$hash]) {$this, serverlist[$hash] = $server;                     } $this->issorted = FALSE;              return TRUE;                     } functionremoveserver ($server) {$hash =mhash ($server);                     if (!isset ($this-serverlist[$hash]) {$this, serverlist[$hash] = $server;                     } $this->issorted = FALSE;              return TRUE;                     } functionlookup ($key) {$hash =mhash ($key);                  if (! $this-issorted) {          Krsort ($this-serverlist,sort_numeric);                            $this-issorted = TRUE; foreach ($this-serverlist as $pos = + $server) {if ($hash >= $pos) return $server                            ;                     } return$this-Serverlist[count ($this-ServerList)-1];       }}} $server = new Flexihash ();       $server->addserver ("192.168.1.1");       $server->addserver ("192.168.1.2");       $server->addserver ("192.168.1.3");       $server->addserver ("192.168.1.4");       $server->addserver ("192.168.1.5");       echo "Save Key1 Inserver:". $server->lookup (' Key1 ');       echo "Save Key2 Inserver:". $server->lookup (' Key2 ');       echo "+++++++++++++++++++++++++++++++++++++++++++++";       $server->removeserver ("192.168.1.4");       echo "Save Key1 Inserver:". $server->lookup (' Key1 '); echo "Save Key2 Inserver: ". $server->lookup (' Key2 ');       echo "+++++++++++++++++++++++++++++++++++++++++++++";       $server->addserver ("192.168.1.6");       echo "Save Key1 Inserver:". $server->lookup (' Key1 ');       echo "Save Key2 Inserver:". $server->lookup (' Key2 ');    echo "+++++++++++++++++++++++++++++++++++++++++++++"; </span>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An example of a consistent hash distribution algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.