天成1 发表于 2018-11-3 13:52:29

C# servicestack.redis 互通 java jedis-13003670

public class Sharded  
    {      private object nodes_lock = new object();      private RedBlackTreeMap nodes = new RedBlackTreeMap();      private IHash hashAlgo = new MD5_LongSUM_Multiply_Hash();
  
      public void AddTarget(int index, string shard)
  
      {            lock (nodes_lock)
  
            {                for (int n = 0; n < 160; ++n)
  
                {                  var hashKey = "SHARD-" + index + "-NODE-" + n;                  long hashValue = this.hashAlgo.Hash(hashKey);
  

  
                  nodes.SetOrAddValue(hashValue, shard);
  
                }
  
            }
  
      }      public string GetShardInfo(string key)
  
      {            long searchHashKey = this.hashAlgo.Hash(key);            long nearestKey;            string shard;            lock (nodes_lock)
  
            {                if (this.nodes.NearestGreater(searchHashKey, out nearestKey))
  
                {
  
                  shard = this.nodes.GetValue(nearestKey);                  return shard;
  
                }                if (this.nodes.Least(out searchHashKey, out shard))                  return shard;
  
            }            throw new Exception("GetShardInfo exception");
  
      }
  
    }


页: [1]
查看完整版本: C# servicestack.redis 互通 java jedis-13003670