Exemplo n.º 1
0
        public static MemcachedService GetInstance(string inst)
        {
            if (m_isOn)
            {
                return(ms);
            }
            ms      = new MemcachedService();
            ms.mc   = ConfigurationManager.GetSection("memcached") as MemCachedConfig;
            ms.pool = SockIOPool.GetInstance(inst);
            ms.pool.SetServers(ms.mc.serverList.ToArray());
            ms.pool.MinConnections       = ms.mc.minPoolSize;
            ms.pool.MaxConnections       = ms.mc.maxPoolSize;
            ms.pool.SocketConnectTimeout = ms.mc.connectionTimeout;
            ms.pool.SocketTimeout        = ms.mc.socketTimeout;
            ms.pool.Initialize();
            ms.mClient                   = new MemcachedClient();
            ms.mClient.PoolName          = inst;
            ms.mClient.EnableCompression = true;
            m_isOn = true;

            return(ms);

            //String[] serverlist = { "118.194.49.34:11211" };
            //pool = SockIOPool.GetInstance("test");
            //pool.SetServers(serverlist);
            //pool.MinConnections = 3;
            //pool.MaxConnections = 10;
            //pool.SocketConnectTimeout = 2000;
            //pool.SocketTimeout = 10000;
            //pool.Initialize();
            //mClient = new MemcachedClient();
            //mClient.PoolName = "test";
            //mClient.EnableCompression = true;
        }
Exemplo n.º 2
0
        static object LOCK_OBJECT = new object();//安全锁定
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static MemcachedHelper()
        {
            InitServer();
            foreach (var k in Servers.Keys)
            {
                SockIOPool pool = SockIOPool.GetInstance(k);
                string[]   s    = { Servers[k] };
                pool.SetServers(s);//设置服务器
                pool.MaxConnections       = 10000;
                pool.MinConnections       = 10;
                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout        = 100;
                pool.Initialize();//初始化缓存线程池
            }
            //默认池
            SockIOPool defaultPool = SockIOPool.GetInstance("DefaultPool");

            defaultPool.SetServers(Servers.Keys.Select(k => Servers[k]).ToArray());//设置服务器
            defaultPool.MaxConnections       = 10000;
            defaultPool.MinConnections       = 10;
            defaultPool.SocketConnectTimeout = 1000;
            defaultPool.SocketTimeout        = 100;
            defaultPool.Initialize(); //初始化默认线程池
            mc = new MemcachedClient {
                PoolName = "DefaultPool"
            };
        }
        /// <summary>
        /// 获取有效的服务器地址
        /// </summary>
        /// <returns>有效的服务器地</returns>
        public static string[] GetConnectedSocketHost()
        {
            SockIO sock          = null;
            string connectedHost = null;

            foreach (string server in servers)
            {
                if (string.IsNullOrEmpty(server))
                {
                    continue;
                }

                try
                {
                    sock = SockIOPool.GetInstance(sockIOPool.Name).GetConnection(server);
                    if (sock != null)
                    {
                        connectedHost = connectedHost + server;
                    }
                }
                finally
                {
                    if (sock != null)
                    {
                        sock.Close();
                    }
                }
            }

            return(connectedHost.Split(','));
        }
Exemplo n.º 4
0
        public MemcachedCache()
        {
            foreach (string info in (WebConfig.GetApp("MemcachedCache.MemcachedServer") ?? string.Empty).Split(';'))
            {
                if (!string.IsNullOrEmpty(info.Trim()))
                {
                    memcachedServer.Add(info);
                }
            }

            if (pool.IsNull() && memcachedServer.Count > 0)
            {
                pool = SockIOPool.GetInstance();
                pool.SetServers(memcachedServer);

                pool.InitConnections      = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.InitConnections") ?? "3");
                pool.MinConnections       = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.MinConnections") ?? "3");
                pool.MaxConnections       = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.MaxConnections") ?? "5");
                pool.SocketConnectTimeout = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.SocketConnectTimeout") ?? "1000");
                pool.SocketTimeout        = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.SocketTimeout") ?? "3000");
                pool.MaintenanceSleep     = Convert.ToInt32(WebConfig.GetApp("MemcachedCache.MaintenanceSleep") ?? "30");
                pool.Failover             = (WebConfig.GetApp("MemcachedCache.Failover") ?? "") == "true" ? true : false;
                pool.Nagle = (WebConfig.GetApp("MemcachedCache.Nagle") ?? "") == "true" ? true : false;
                //pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;
                pool.Initialize();

                mc                   = new MemcachedClient();
                mc.PoolName          = WebConfig.GetApp("MemcachedCache.PoolName") ?? "default";
                mc.EnableCompression = (WebConfig.GetApp("MemcachedCache.EnableCompression") ?? "") == "true" ? true : false;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        public MemcacheProvider()
        {
            string text = ConfigurationManager.AppSettings["MemcachedHost"];

            if (text != null)
            {
                this.servers = text.ToString().Split(new char[]
                {
                    ','
                });
            }
            this.pool = SockIOPool.GetInstance();
            this.pool.SetServers(this.servers);
            this.pool.InitConnections      = 100;
            this.pool.MinConnections       = 3;
            this.pool.MaxConnections       = 1024;
            this.pool.SocketConnectTimeout = 1000;
            this.pool.SocketTimeout        = 3000;
            this.pool.MaintenanceSleep     = 30L;
            this.pool.Failover             = true;
            this.pool.Nagle = false;
            this.pool.Initialize();
            this.mc = new MemcachedClient
            {
                EnableCompression = false
            };
        }
Exemplo n.º 6
0
        static MemcacheHelper()
        {
            //string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };//should in config。
            string[] serverlist = ConfigurationManager.AppSettings["MemcacheConfig"].Split(',');

            //init pool
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // get client instance
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 自动货源切换管理服务
        /// </summary>
        static void Main()
        {
            SockIOPool pool2 = SockIOPool.GetInstance("Price_Cache");

            string[] serverlist2 = ConfigurationManager.AppSettings["ServerList2"].Split(',');
            pool2.SetServers(serverlist2);
            pool2.SetWeights(new int[] { 1 });
            pool2.InitConnections      = 5;
            pool2.MinConnections       = 5;
            pool2.MaxConnections       = 280;
            pool2.MaxIdle              = 1000 * 60 * 60 * 6;
            pool2.SocketTimeout        = 1000 * 3;
            pool2.SocketConnectTimeout = 0;
            pool2.SocketTimeout        = 3000;
            pool2.MaintenanceSleep     = 60;
            pool2.Failover             = true;
            pool2.Nagle   = false;
            pool2.MaxBusy = 1000 * 10;
            pool2.Initialize();

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
            ServiceBase.Run(ServicesToRun);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "192.168.1.9:11211", "192.168.202.128:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(servers);
            pool.InitConnections      = 3;
            pool.MinConnections       = 3;
            pool.MaxConnections       = 5;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;
            pool.MaintenanceSleep     = 30;
            pool.Failover             = true;
            pool.Nagle = false;
            pool.Initialize();
            //客户端实例
            MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();

            mc.EnableCompression = false;

            mc.Add("key1", "This is my value", DateTime.Now.AddMinutes(20));
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        static MemcachedHelper()
        {
            string servers = "Memcached.Servers".GetAppSetting();

            string[] serverList = new string[] { servers };
            if (servers.Contains(","))
            {
                serverList = servers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }

            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverList);
            pool.InitConnections      = serverList.Length;
            pool.MinConnections       = serverList.Length;
            pool.MaxConnections       = 100;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;
            pool.MaintenanceSleep     = 30;
            pool.Failover             = true;
            pool.Nagle = false;
            pool.Initialize();

            mClient = new MemcachedClient();
            mClient.EnableCompression = false;
        }
Exemplo n.º 10
0
        public MemCache(string[] strServer, string pre)
        {
            preFix = pre;
            try
            {
                pool = SockIOPool.GetInstance();
                pool.SetServers(strServer);

                //初始化连接数
                pool.InitConnections = 3;
                //最小连接数
                pool.MinConnections = 3;
                //最大连接数
                pool.MaxConnections = 5;


                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout        = 3000;

                pool.MaintenanceSleep = 30;
                pool.Failover         = true;

                pool.Nagle = false;
                pool.Initialize();
            }
            catch (Exception ex)
            {
                //TODO 记录日志
                throw ex;
            }
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            //就是构建了一个集群 安装memcached 的使用 我们是一个客户端使用
            //在实际项目中使用 可以在哪些地方使用 有一个唯一的KeyValue 来获取一个单独的数据
            //项目中玩耍的东西。。
            string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            MemcachedClient mc = new MemcachedClient();

            mc.EnableCompression = false;

            Console.WriteLine("------------测  试-----------");
            mc.Set("test", "my value"); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"

            if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
            {
                Console.WriteLine("test is Exists");
                Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
            }
            else
            {
                Console.WriteLine("test not Exists");
            }

            Console.ReadLine();

            mc.Delete("test");  //移除缓存中key为test的项目

            if (mc.KeyExists("test"))
            {
                Console.WriteLine("test is Exists");
                Console.WriteLine(mc.Get("test").ToString());
            }
            else
            {
                Console.WriteLine("test not Exists");
            }
            Console.ReadLine();

            SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
        }
Exemplo n.º 12
0
        static MemcacheHelper()
        {
            //最好放在配置文件中
            string localIP = ComputerHelper.GetLocalIP();

            string[] serverList = { localIP + ":11211", "10.0.0.137:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverList);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            //获得客户端实例
            m_memcachedClient = new MemcachedClient();
            m_memcachedClient.EnableCompression = false;
        }
Exemplo n.º 13
0
        /// <summary>
        /// 获取有效的服务器地址
        /// </summary>
        /// <returns>有效的服务器地</returns>
        public static string[] GetConnectedSocketHost()
        {
            SockIO sock          = null;
            string connectedHost = null;

            foreach (string hostName in serverList)
            {
                if (!Discuz.Common.Utils.StrIsNullOrEmpty(hostName))
                {
                    try
                    {
                        sock = SockIOPool.GetInstance(memCachedConfigInfo.PoolName).GetConnection(hostName);
                        if (sock != null)
                        {
                            connectedHost = Discuz.Common.Utils.MergeString(hostName, connectedHost);
                        }
                    }
                    finally
                    {
                        if (sock != null)
                        {
                            sock.Close();
                        }
                    }
                }
            }
            return(Discuz.Common.Utils.SplitString(connectedHost, ","));
        }
Exemplo n.º 14
0
        static MemcacheHelper()
        {
            //最好放在配置文件中
            string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
Exemplo n.º 15
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">hostname:port</param>
        /// <param name="timeout">read timeout value for connected socket</param>
        /// <param name="connectTimeout">timeout for initial connections</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            String[] ip = host.Split(':');

            // get socket: default is to use non-blocking connect
            if (connectTimeout > 0)
            {
                _socket = GetSocket(ip[0], int.Parse(ip[1], new System.Globalization.NumberFormatInfo()), connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(ip[0]), int.Parse(ip[1], new System.Globalization.NumberFormatInfo())));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host;
        }
Exemplo n.º 16
0
        public MemcachedWriter()
        {
            //初始化memcached
            //分布Memcachedf服务IP 端口
            string[] servers = System.Configuration.ConfigurationManager.AppSettings["MemcachedAddress"].Split(',');;
            //string[] servers = { "192.168.5.19:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(servers);
            pool.InitConnections      = 3;
            pool.MinConnections       = 3;
            pool.MaxConnections       = 5;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;
            pool.MaintenanceSleep     = 30;
            pool.Failover             = true;
            pool.Nagle = false;
            pool.Initialize();
            //客户端实例
            MemcachedClient mc1 = new MemcachedClient();

            mc1.EnableCompression = false;

            mc = mc1;
        }
Exemplo n.º 17
0
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">host to connect to</param>
        /// <param name="port">port to connect to</param>
        /// <param name="timeout">int ms to block on data for read</param>
        /// <param name="connectTimeout">timeout (in ms) for initial connection</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int port, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            if (connectTimeout > 0)
            {
                _socket = GetSocket(host, port, connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(host), port));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host + ":" + port;
        }
Exemplo n.º 18
0
        /// <summary>
        /// session构造函数
        /// </summary>
        /// <param name="strServer">服务器</param>
        /// <param name="sessionHour">session保存时间,单位为小时</param>
        /// <param name="cookieDomain">给cookie指定域名,用于跨域访问</param>
        public MySession(string[] strServer, int sessionHour, string cookieDomain, string pre)
        {
            mc                = new MemcachedClient();
            preFix            = pre;
            this.sessionHour  = sessionHour;
            this.cookieDomain = cookieDomain;
            pool              = SockIOPool.GetInstance();
            pool.SetServers(strServer);

            //初始化连接数
            pool.InitConnections = 3;
            //最小连接数
            pool.MinConnections = 3;
            //最大连接数
            pool.MaxConnections = 20;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();
        }
Exemplo n.º 19
0
		public static void Main(string[] args) 
		{
			String[] serverlist = { "140.192.34.72:11211", "140.192.34.73:11211"  };

			// initialize the pool for memcache servers
			SockIOPool pool = SockIOPool.GetInstance("test");
			pool.SetServers(serverlist);
			pool.Initialize();

			mc = new MemcachedClient();
			mc.PoolName = "test";
			mc.EnableCompression = false;

			test1();
			test2();
			test3();
			test4();
			test5();
			test6();
			test7();
			test8();
			test9();
			test10();
			test11();
			test12();
			test13();
			test14();

			pool.Shutdown();
		}
Exemplo n.º 20
0
        public bool MemcacheClientInit()
        {
            this.nosqlTb.Text += "MemcacheClient Init Started!\r\n";

            string[] serverlist = { "127.0.0.1:11211", "90.0.12.120:11211" };

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();
            this.nosqlTb.Text += "MemcacheClient Init End!\r\n";
            return(true);
        }
Exemplo n.º 21
0
 //初始化缓存
 static void MemClientInit()
 {
     try
     {
         MemClient = CallContext.GetData("client") as MemcachedClient;
         if (MemClient == null)
         {
             string   strAppMemcachedServer = System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"];
             string[] servers = strAppMemcachedServer.Split(',');
             //初始化池
             SockIOPool pool = SockIOPool.GetInstance();
             pool.SetServers(servers);
             pool.InitConnections      = 3;
             pool.MinConnections       = 3;
             pool.MaxConnections       = 5000;
             pool.SocketConnectTimeout = 1000;
             pool.SocketTimeout        = 3000;
             pool.MaintenanceSleep     = 30;
             pool.Failover             = true;
             pool.Nagle = false;
             pool.Initialize();
             //客户端实例
             MemClient = new MemcachedClient();
             MemClient.EnableCompression = false;
             CallContext.SetData("client", MemClient);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 22
0
        public MemcacheWriter()
        {
            //分布Memcachedf服务IP 端口
            //string[] servers = { "192.168.1.100:11211", "192.168.1.2:11211" };
            string strAppMemcachedServer = System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"];

            string[] servers = strAppMemcachedServer.Split(',');
            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(servers);
            pool.InitConnections      = 3;
            pool.MinConnections       = 3;
            pool.MaxConnections       = 5;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;
            pool.MaintenanceSleep     = 30;
            pool.Failover             = true;
            pool.Nagle = false;
            pool.Initialize();
            //客户端实例
            MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();

            mc.EnableCompression = false;
            memcachedClient      = mc;
        }
Exemplo n.º 23
0
 /// <summary>
 /// 受保护类型的缓存对象,初始化一个新的缓存对象
 /// </summary>
 protected MemcacheHelper()
 {
     //读取app.Config中需要缓存的服务器地址信息,可以传递多个地址,使用","分隔
     string[] serverList = ConfigurationManager.AppSettings["Memcached.ServerList"].Split(',');
     try
     {
         var sockIoPool = SockIOPool.GetInstance();
         sockIoPool.SetServers(serverList);
         sockIoPool.InitConnections      = 3;
         sockIoPool.MinConnections       = 3;
         sockIoPool.MaxConnections       = 50;
         sockIoPool.SocketConnectTimeout = 1000;
         sockIoPool.SocketTimeout        = 3000;
         sockIoPool.MaintenanceSleep     = 30;
         sockIoPool.Failover             = true;
         sockIoPool.Nagle = false;
         //实例化缓存对象
         _client = new MemcachedClient();
     }
     catch (Exception ex)
     {
         //错误信息写入事务日志
         throw new Exception(ex.Message);
     }
 }
Exemplo n.º 24
0
        public static MemcachedClient GetCurrentMemClient2()
        {
            if (client == null)
            {
                string   strAppMemcachedServer = System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"];
                string[] servers = strAppMemcachedServer.Split(',');
                //初始化池
                SockIOPool pool = SockIOPool.GetInstance();
                pool.SetServers(servers);
                pool.InitConnections      = 3;
                pool.MinConnections       = 3;
                pool.MaxConnections       = 500;
                pool.SocketConnectTimeout = 1000;
                pool.SocketTimeout        = 3000;
                pool.MaintenanceSleep     = 30;
                pool.Failover             = true;
                pool.Nagle = false;
                pool.Initialize();
                //客户端实例
                client = new MemcachedClient();
                client.EnableCompression = false;
            }

            return(client);
        }
Exemplo n.º 25
0
        static MemcacheHelper()
        {
            //最好放在配置文件中
            //string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };
            string strMemcacheServerlist = ConfigHelper.GetAppSettings("MemcacheServerlist");

            if (string.IsNullOrEmpty(strMemcacheServerlist))
            {
                throw new Exception("未在配置文件的<AppSettings>下配置MemcacheServerlist;eg:<add key=\"MemcacheServerlist\" value=\"127.0.0.1:11211, 10.0.0.132:11211\" />");
            }
            string[] serverlist = strMemcacheServerlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
Exemplo n.º 26
0
        /// <summary>
        /// 入口
        /// </summary>
        /// <returns></returns>
        public static CompanyRightCached getInstance()
        {
            if (_objInstance == null)
            {
                lock (_object)
                {
                    if (_objInstance == null)
                    {
                        _objInstance = new CompanyRightCached();
                        //initialize the pool for memcache servers
                        SockIOPool pool = SockIOPool.GetInstance("CompanyRightCached");

                        string cServerList = System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"] == null ?
                                             string.Empty : System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"];
                        if (cServerList != string.Empty)
                        {
                            _serverlist = cServerList.Split(',');
                        }
                        pool.SetServers(_serverlist);
                        pool.Initialize();

                        _mc                   = new MemcachedClient();
                        _mc.PoolName          = "CompanyRightCached";
                        _mc.EnableCompression = false;
                    }
                }
            }
            return(_objInstance);
        }
Exemplo n.º 27
0
        static MemcacheWriter()
        {
            string strAppMemcache = System.Configuration.ConfigurationManager.AppSettings["MemcachedServerList"];

            if (string.IsNullOrEmpty(strAppMemcache))
            {
                throw new Exception("Mencache配置文件未配置!");
            }
            string[] serverlist = strAppMemcache.Split(',');

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
Exemplo n.º 28
0
        static MemcacheHelper()
        {
            string[] serverlist = { "60.18.162.202:11211" };//一定要将地址写到Web.config文件中。
            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();

            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections  = 3;
            pool.MaxConnections  = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover         = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
Exemplo n.º 29
0
        /// <summary>
        /// 获取有效的服务器地址
        /// </summary>
        /// <returns>有效的服务器地</returns>
        public static string[] GetConnectedSocketHost()
        {
            SockIO sock          = null;
            string connectedHost = null;

            foreach (string hostName in serverList)
            {
                if (!string.IsNullOrEmpty(hostName))
                {
                    try
                    {
                        sock = SockIOPool.GetInstance(memCachedConfigInfo.PoolName).GetConnection(hostName);
                        if (sock != null)
                        {
                            connectedHost = hostName + "," + connectedHost;
                        }
                    }
                    finally
                    {
                        if (sock != null)
                        {
                            sock.Close();
                        }
                    }
                }
            }
            return(connectedHost.Split(','));
        }
Exemplo n.º 30
0
        private void start(params string[] servers)
        {
            string[] serverlist;
            if (servers == null || servers.Length < 1)
            {
                serverlist = new string[] { "127.0.0.1:11011" }; //服务器列表,可多个
            }
            else
            {
                serverlist = servers;
            }
            pool = SockIOPool.GetInstance();

            //根据实际情况修改下面参数
            pool.SetServers(serverlist);
            pool.InitConnections      = 3;
            pool.MinConnections       = 3;
            pool.MaxConnections       = 5;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout        = 3000;
            pool.MaintenanceSleep     = 30;
            pool.Failover             = true;
            pool.Nagle = false;
            pool.Initialize(); // initialize the pool for memcache servers
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:MemcachedClient"/> using the specified configuration instance.
        /// </summary>
        /// <param name="configuration">The memcachedClient configuration.</param>
        public static MemcachedClient CreateClient(IMemcachedClientConfiguration configuration)
        {
            if (configuration == null)
                throw new ArgumentNullException("configuration");

            sockIOPool = configuration.CreatePool();
            servers = configuration.Servers.Select(ip => ip.Address.ToString() + ":" + ip.Port).ToArray();

            memcachedClient = new MemcachedClient();
            memcachedClient.PoolName = sockIOPool.Name;
            memcachedClient.EnableCompression = false;

            return memcachedClient;
        }
        private static void CreateManager()
        {
            serverList = memCachedConfigInfo.ServerList;
            pool = SockIOPool.GetInstance(memCachedConfigInfo.PoolName);
            pool.SetServers(serverList);
            pool.InitConnections = memCachedConfigInfo.IntConnections;//初始化链接数
            pool.MinConnections = memCachedConfigInfo.MinConnections;//最少链接数
            pool.MaxConnections = memCachedConfigInfo.MaxConnections;//最大连接数
            pool.SocketConnectTimeout = memCachedConfigInfo.SocketConnectTimeout;//Socket链接超时时间
            pool.SocketTimeout = memCachedConfigInfo.SocketTimeout;// Socket超时时间
            pool.MaintenanceSleep = memCachedConfigInfo.MaintenanceSleep;//维护线程休息时间
            pool.Failover = memCachedConfigInfo.FailOver; //失效转移(一种备份操作模式)
            pool.Nagle = memCachedConfigInfo.Nagle;//是否用nagle算法启动socket
            pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;

            pool.Initialize();

            mc = new MemcachedClient();
            mc.PoolName = memCachedConfigInfo.PoolName;
            mc.EnableCompression = false;
        }