/// <summary>
/// 獲取一個key的Md5 Hash值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public int HashCode(string key)
{
int HashCode;
byte[] Md5Bype;
if (Md5Cache.ContainsKey(key))
{
return Md5Cache[key];
}
else
{
using (var md5 = new MD5CryptoServiceProvider())
{
Md5Bype = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
}
HashCode = BitConverter.ToInt32(Md5Bype, 0);
if (HashCode < 0)
{
HashCode = int.MaxValue + HashCode;
}
HashCode = Math.Abs(HashCode);
Md5Cache[key] = HashCode;
}
return HashCode;
}
/// <summary>
/// 是否是當前節點
/// </summary>
public int BeloneNode(string key)
{
return HashCode(key) % NodeCount;
}