Exemplo n.º 1
0
        /// <summary>
        /// Disposes of a pool.
        /// </summary>
        public void Dispose()
        {
            lock (this)
            {
                if (pool != null)
                {
                    pool.Shutdown();
                    DisposeSockIOPool(poolName, pool);

                    pool = null;
                }
            }
        }
Exemplo n.º 2
0
 public static void Dispose()
 {
     if (pool != null)
     {
         pool.Shutdown();
     }
 }
Exemplo n.º 3
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();
		}
 public static void Dispose()
 {
     if (sockIOPool != null)
     {
         sockIOPool.Shutdown();
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 释放池中资源
 /// </summary>
 public static void Dispose()
 {
     if (MemCachedConfig.ApplyMemCached && pool != null)
     {
         pool.Shutdown();
     }
 }
Exemplo n.º 6
0
 private static void Shutdown()
 {
     if (_pool != null)
     {
         _pool.Shutdown();
     }
 }
Exemplo n.º 7
0
        public void MemcachedClientTest()
        {
            // 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();

            pool.Shutdown();
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public void Stop()
        {
            // Needs to lock staticly because the pool and the internal maintenance thread
            // are both static, and I want them syncs between starts and stops.
            lock (syncObject)
            {
                SockIOPool pool = SockIOPool.GetInstance(MemCacheClient.PoolName);
                if (pool.Initialized)
                {
                    pool.Shutdown();
                }

                // The maintenance thread must be set to null in the hard way
                // Due to a bug in the memcached client, the thread is not set to null which causes the
                // pool to fail if it needs to be restarted. Hopefully this method is not called too many times
                // (only when the SessionFactory is closed)
                FieldInfo maintenanceThread = typeof(SockIOPool).GetField("_maintenanceThread",
                                                                          BindingFlags.NonPublic | BindingFlags.Instance);
                maintenanceThread.SetValue(pool, null);
            }
        }
Exemplo n.º 9
0
 public void Shutdown()
 {
     if (_pool != null)
     {
         _pool.Shutdown();//very important. otherwise, the app will not be termitated after closing the window
     }
     _pool = null;
     //after leave this function, _cmdMemcachedProcess will be automatic exited by app lifecycle
     if (_cmdMemcachedProcess != null)
     {
         _cmdMemcachedProcess.StandardInput.WriteLine(_memcachedInWinFolder + " -d stop");
         System.Threading.Thread.Sleep(250);
         _cmdMemcachedProcess.StandardInput.WriteLine("exit");
         System.Threading.Thread.Sleep(250);
         _cmdMemcachedProcess.Close();
     }
     _cmdMemcachedProcess = null;
     IsActived            = false;
     if (MC != null)
     {
         MC.FlushAll(); MC = null;
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// @brief:终止serverList中的Memcached的客户端连接池
        /// </summary>
        /// <param name="serverList"></param>
        /// <param name="poolNames"></param>
        /// <returns></returns>
        public static bool StopMemcachePool(ArrayList serverList, ArrayList poolNames)
        {
            if (serverList == null || poolNames == null)
            {
                throw new ArgumentNullException("Argument Must Not Null");
            }

            if (serverList.Count != poolNames.Count)
            {
                throw new ArgumentException("serverList.Count Must Equals poolNames.Count");
            }

            for (int i = 0; i < serverList.Count; i++)
            {
                SockIOPool pool = SockIOPool.GetInstance(poolNames[i].ToString());
                if (pool != null)
                {
                    pool.Shutdown();
                }
            }

            return(true);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            //参数设置
            string SockIOPoolName = "Test_SockIOPoolName";

            //分布Memcached服务IP 端口
            string[] MemcacheServiceList = { "127.0.0.1:11211" };

            //设置连接池
            SockIOPool SPool = SockIOPool.GetInstance(SockIOPoolName);

            SPool.SetServers(MemcacheServiceList);
            SPool.MaxConnections       = 5;
            SPool.MinConnections       = 3;
            SPool.InitConnections      = 3;
            SPool.SocketConnectTimeout = 1000;
            SPool.SocketTimeout        = 3000;
            SPool.Failover             = true;
            SPool.Nagle = false;
            SPool.Initialize();

            //实例化Client
            MemcachedClient MClient = new MemcachedClient();

            MClient.PoolName = SockIOPoolName;
            MClient.FlushAll();

            Console.WriteLine("1.创建memcache缓存Hello World");
            MClient.Add("Key1001", "Hello World");
            Console.WriteLine("2.查询缓存信息{0}", MClient.Get("Key1001"));

            Console.WriteLine("3.修改memcache缓存Hello World");
            MClient.Set("Key1001", "Hello World - 修改版");
            Console.WriteLine("4.查询缓存信息{0}", MClient.Get("Key1001"));

            if (MClient.KeyExists("Key1001"))
            {
                Console.WriteLine("5.删除memcache缓存");
                MClient.Delete("Key1001");
            }

            if (MClient.KeyExists("Key1001"))
            {
                Console.WriteLine(MClient.Get("Key1001"));
            }
            else
            {
                Console.WriteLine("6.删除已删除");
            }

            Student stud = new Student()
            {
                id = "10001", name = "张三"
            };

            MClient.Add("student", stud);
            Student Get_stud = MClient.Get("student") as Student;

            Console.WriteLine("6.缓存实体对象:{0} {1}", Get_stud.id, Get_stud.name);

            MClient.Add("Key1002", "我已设置过期时间1分钟", DateTime.Now.AddMinutes(1));
            while (true)
            {
                if (MClient.KeyExists("Key1002"))
                {
                    Console.WriteLine("key:Key1002 Value:{0},当前时间:{1}", MClient.Get("Key1002"), DateTime.Now);
                    Thread.Sleep(20000);
                }
                else
                {
                    Console.WriteLine("key:Key1002 我已过期,当前时间:{0}", DateTime.Now);
                    break;
                }
            }
            SPool.Shutdown();
        }
Exemplo n.º 12
0
 public void UnInit()
 {
     pool.Shutdown();
     m_isOn = false;
 }
Exemplo n.º 13
0
        //max memory to use for memcached items in megabytes, default is 64 MB
        public MemCache(int memorySize)
        {
            //the goal is copy memcached.exe to %windir% folder, then from there, install memcached windows service so that the executable path of the service is %windir%\memcached.exe. Thus, PBS folder could be moved to anywhere else.
            string strCmdResult;

            try
            {
                //check if memcached.exe,msvcr71.dll exists in PBS folder
                if (!File.Exists("memcached.exe"))
                {
                    throw new Exception("memcached.exe doesn't exists!");
                }
                if (!File.Exists("msvcr71.dll"))
                {
                    throw new Exception("msvcr71.dll doesn't exists!");
                }
                _memcachedInWinFolder = Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\memcached.exe";
                //check if memcached.exe,pthreadGC2.dll exists in windows path
                if (!File.Exists(_memcachedInWinFolder))
                {
                    File.Copy("memcached.exe", _memcachedInWinFolder, true);
                }
                if (!File.Exists(Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\msvcr71.dll"))
                {
                    File.Copy("msvcr71.dll", Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\msvcr71.dll", true);
                }
                //if windows service exists, check if the exe path of the service is in windows directory
                if (Utility.IsWindowsServiceExisted("memcached Server"))
                {
                    Utility.Cmd("sc qc \"memcached Server\"", false, out strCmdResult);
                    if (!strCmdResult.Contains("\\Windows\\"))
                    {
                        Utility.Cmd("sc delete \"memcached Server\"", false, out strCmdResult);//try to uninstall windows service
                    }
                }
                //check if windows service exists
                if (!Utility.IsWindowsServiceExisted("memcached Server"))
                {
                    Utility.Cmd(_memcachedInWinFolder + " -d install", false, out strCmdResult);          //install memcached windows service
                    //google:使用cmd命令手动、自动启动和禁用服务
                    Utility.Cmd("sc config \"memcached Server\" start= demand", false, out strCmdResult); //set to 手动启动
                }
                Utility.Cmd(_memcachedInWinFolder + " -d stop", false, out strCmdResult);
                _cmdMemcachedProcess = Utility.Cmd(_memcachedInWinFolder + " -m " + memorySize + " -d start", true, out strCmdResult);
                string[] serverlist = { "127.0.0.1:11211" };

                // initialize the pool for memcache 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();
                if (_pool.GetConnection(serverlist[0]) == null)
                {
                    _pool.Shutdown();
                    Utility.Cmd("sc delete \"memcached Server\"", false, out strCmdResult);//try to uninstall windows service
                    throw new Exception("Can not managed to run 'memcached -d start' on your machine.");
                }

                MC = new MemcachedClient()
                {
                    EnableCompression = false
                };
                //try to cache one object in memory to ensure memcached ability can be used
                if (!MC.Set("test", DateTime.Now.ToString()))
                {
                    throw new Exception("Can't managed to set key-value in memched!");
                }
                IsActived = true;
            }
            catch (Exception e)
            {
                IsActived = false;
                Shutdown();//important
                //copy error:"Access to the path 'C:\Windows\system32\memcached.exe' is denied."
                if (e.Message.Contains("Access") && e.Message.Contains("denied"))
                {
                    throw new Exception("Copy memcached.exe to '%windir%' failed.\r\nPlease reopen PortableBasemapServer by right clicking and select 'Run as Administrator'.");
                }
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            // Memcached服务器列表
            // 如果有多台服务器,则以逗号分隔,例如:"192.168.80.10:11211","192.168.80.11:11211"
            string[] serverList = { "192.168.0.103:11211" };
            // 初始化SocketIO池
            string     poolName   = "MyPool";
            SockIOPool sockIOPool = SockIOPool.GetInstance(poolName);

            // 添加服务器列表
            sockIOPool.SetServers(serverList);
            // 设置连接池初始数目
            sockIOPool.InitConnections = 3;
            // 设置连接池最小连接数目
            sockIOPool.MinConnections = 3;
            // 设置连接池最大连接数目
            sockIOPool.MaxConnections = 5;
            // 设置连接的套接字超时时间(单位:毫秒)
            sockIOPool.SocketConnectTimeout = 1000;
            // 设置套接字超时时间(单位:毫秒)
            sockIOPool.SocketTimeout = 3000;
            // 设置维护线程运行的睡眠时间:如果设置为0,那么维护线程将不会启动
            sockIOPool.MaintenanceSleep = 30;
            // 设置SockIO池的故障标志
            sockIOPool.Failover = true;
            // 是否用nagle算法启动
            sockIOPool.Nagle = false;
            // 正式初始化容器
            sockIOPool.Initialize();

            // 获取Memcached客户端实例
            MemcachedClient memClient = new MemcachedClient();

            // 指定客户端访问的SockIO池
            memClient.PoolName = poolName;
            // 是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被储存在压缩的形式
            memClient.EnableCompression = false;

            Console.WriteLine("----------------------------测试开始----------------------------");

            // 01.简单的添加与读取操作
            memClient.Set("test1", "edisonchou");
            Console.WriteLine("test1:{0}", memClient.Get("test1"));
            // 02.先添加后修改再读取操作
            memClient.Set("test2", "jacky");
            Console.WriteLine("test2:{0}", memClient.Get("test2"));
            memClient.Set("test2", "edwin");
            Console.WriteLine("test2:{0}", memClient.Get("test2"));
            memClient.Replace("test2", "lousie");
            Console.WriteLine("test2:{0}", memClient.Get("test2"));
            // 03.判断Key值是否存在
            if (memClient.KeyExists("test2"))
            {
                Console.WriteLine("Key:test2 is existed");
            }
            // 04.删除指定Key值的数据
            memClient.Add("test3", "memcached");
            Console.WriteLine("test3:{0}", memClient.Get("test3"));
            memClient.Delete("test3");
            if (!memClient.KeyExists("test3"))
            {
                Console.WriteLine("Key:test3 is not existed");
            }
            // 05.设置数据过期时间:5秒后过期
            memClient.Add("test4", "expired", DateTime.Now.AddSeconds(5));
            Console.WriteLine("test4:{0}", memClient.Get("test4"));
            Console.WriteLine("Please waiting the sleeping time");
            System.Threading.Thread.Sleep(6000);
            if (!memClient.KeyExists("test4"))
            {
                Console.WriteLine("test4 is expired");
            }
            Console.WriteLine("----------------------------测试完成----------------------------");

            // 关闭SockIO池
            sockIOPool.Shutdown();

            Console.ReadKey();
        }
Exemplo n.º 15
0
 protected void OnDisposed()
 {
     mPool.Shutdown();
 }