예제 #1
0
        /// <summary>
        /// 根据缓存Key获取其所存储的服务器信息
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>Key所存储的服务器信息</returns>
        public static string GetHostInfoByCacheKey(string key)
        {
            string hostInfo = "";

            SockIO sock = null;

            try
            {
                sock = SockIOPool.GetInstance(SocketPoolName).GetSock(key);

                if (sock != null)
                {
                    hostInfo = sock.Host;
                }
            }
            finally
            {
                if (sock != null)
                {
                    sock.Close();
                }
            }

            return(hostInfo);
        }
예제 #2
0
        /// <summary>
        /// 初始化相关配置项
        /// </summary>
        static void Initialization()
        {
            #region 加载配置文件

            MemcachedConfigSection memcachedConfig = (MemcachedConfigSection)ConfigurationManager.GetSection("memcached");

            if (memcachedConfig == null)
            {
                throw new ConfigurationErrorsException("未找到memcached对应的配置节点,请检查配置文件!");
            }

            IsMemcachedEnabled = memcachedConfig.Enabled;

            if (memcachedConfig.Servers == null || memcachedConfig.Servers.Count < 1)
            {
                throw new ConfigurationErrorsException("未找到任何服务器配置信息,请检查配置文件!");
            }

            ServerList = new string[memcachedConfig.Servers.Count];

            for (int i = 0; i < memcachedConfig.Servers.Count; i++)
            {
                ServerList[i] = string.Concat(memcachedConfig.Servers[i].ServerIP, ":", memcachedConfig.Servers[i].Port);
            }

            if (memcachedConfig.SocketPool == null)
            {
                throw new ConfigurationErrorsException("未找到Socket连接串配置信息,请检查配置文件!");
            }

            SocketPoolName = memcachedConfig.SocketPool.PoolName;

            #endregion

            #region 初始化Socket连接串

            pool = SockIOPool.GetInstance(SocketPoolName);

            pool.SetServers(ServerList);

            // 初始化链接数
            pool.InitConnections = memcachedConfig.SocketPool.MinConnections;

            // 最少链接数
            pool.MinConnections = memcachedConfig.SocketPool.MinConnections;

            // 最大连接数
            pool.MaxConnections = memcachedConfig.SocketPool.MaxConnections;

            // Socket链接超时时间
            pool.SocketConnectTimeout = memcachedConfig.SocketPool.SocketConnectTimeout;

            // Socket超时时间
            pool.SocketTimeout = memcachedConfig.SocketPool.SocketTimeout;

            // 维护线程休眠时间
            pool.MaintenanceSleep = memcachedConfig.SocketPool.MaintenanceSleep;

            // 失效转移(一种备份操作模式)
            pool.Failover = memcachedConfig.SocketPool.Failover;

            // 是否用nagle算法启动socket
            pool.Nagle = memcachedConfig.SocketPool.Nagle;

            // 散列算法
            pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;

            pool.Initialize();

            #endregion

            #region 初始化Memcached客户端

            client = new MemcachedClient();

            client.PoolName = SocketPoolName;

            // 是否启用压缩
            client.EnableCompression = false;

            #endregion
        }