示例#1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="name"></param>
 /// <param name="scope"></param>
 /// <param name="servers"></param>
 internal MemcachedProxy(string name, ServerScope scope, IList <ServerNode> servers)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ConfigurationErrorsException("Name of MemcachedClient instance cannot be empty.");
     }
     if ((servers == null) || (servers.Count == 0))
     {
         throw new ConfigurationErrorsException("Cannot configure MemcachedClient with empty list of hosts.");
     }
     this.Name       = name;
     this.KeyPrefix  = string.Format("_{0}_", name);
     this.serverPool = new ServerPool(servers, scope);
 }
示例#2
0
        internal ServerPool(IList <ServerNode> servers, ServerScope scope)
        {
            this.Scope        = scope;
            this.slavePoolMap = new Dictionary <string, SocketPool[]>();
            hostDictionary    = new Dictionary <uint, SocketPool>();
            List <SocketPool> pools = new List <SocketPool>();
            List <uint>       keys  = new List <uint>();

            foreach (var server in servers)
            {
                //Create pool
                SocketPool pool = new SocketPool(this, server.Address.Trim());
                if (server.Slaves != null && server.Slaves.Count > 0)
                {
                    var slaveList = new SocketPool[server.Slaves.Count];

                    for (var i = 0; i < server.Slaves.Count; i++)
                    {
                        slaveList[i] = new SocketPool(this, server.Slaves[i].Address.Trim());
                    }

                    this.slavePoolMap[server.Address] = slaveList;
                }

                //Create 250 keys for this pool, store each key in the hostDictionary, as well as in the list of keys.
                for (int i = 0; i < 250; i++)
                {
                    uint key = BitConverter.ToUInt32(new ModifiedFNV1_32().ComputeHash(Encoding.UTF8.GetBytes(server.Address + "-" + i)), 0);
                    if (!hostDictionary.ContainsKey(key))
                    {
                        hostDictionary[key] = pool;
                        keys.Add(key);
                    }
                }

                pools.Add(pool);
            }

            //Hostlist should contain the list of all pools that has been created.
            hostList = pools.ToArray();

            //Hostkeys should contain the list of all key for all pools that have been created.
            //This array forms the server key continuum that we use to lookup which server a
            //given item key hash should be assigned to.
            keys.Sort();
            hostKeys = keys.ToArray();
        }