Consistency Ring hash algorithm has a big use is to solve the problem of Memcache server down machine. The goal is to increase or remove the Memcache server, minimizing the impact.
The theory is not introduced, there is too much information on the Internet, please search your own search.
A consistencyring class is written here to implement the algorithm.
The test classes are as follows:
public static void Test () {{consistencyring CR = new consistencyring ();
Console.WriteLine ("=============addserver test============="); Cr.
Addserver ("1"); Cr.
Addserver ("5"); Cr.
Addserver ("9"); Cr.
Addserver ("11"); Cr.
Addserver ("3");
Console.WriteLine ("=============removeserver test============="); Cr.
Removeserver ("5"); Cr.
Removeserver ("3"); Cr.
Removeserver ("1"); Cr.
Removeserver ("9");
{consistencyring CR = new consistencyring (); Cr.
Addserver ("1"); Cr.
Addserver ("5"); Cr.
Addserver ("9"); Cr.
Addserver ("11"); Cr.
Addserver ("3");
Console.WriteLine ("=============getserverip test============="); String keY
String ServerIP;
Key = "A1";
ServerIP = Cr[key];
Console.WriteLine ("A1 IP:" + serverip); Cr.
Removeserver ("9");
ServerIP = Cr[key];
Console.WriteLine ("A1 IP:" + serverip); }
}
The parameters in the addserver/removeserver above are changed to a concise numeric string for demonstration purposes and can be transmitted using string such as 192.168.1.1.