Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a wrapper for an existing <see cref="MemcachedClient" />.
        /// </summary>
        /// <param name="client">The existing client</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="client"/> is null</exception>
        public MemcachedCache(MemcachedClient client)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            this.client = client;
        }
Ejemplo n.º 3
0
        static MemcacheHelper()
        {
            string ip = ConfigurationManager.AppSettings["IPAddressPort"];
            //string[] serverlist = { "192.168.255.250:11211", "101.0.0.1:11211" };

            string[] serverlist = ip.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();

            // 获得客户端实例
            ServerList = new MemcachedClient();
            ServerList.EnableCompression = false;
        }
Ejemplo n.º 4
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);
     }
 }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "192.168.1.100:11211", "192.168.1.2: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("Key", "黑马8期大鹏");
            Console.WriteLine("Ok");

            // mc.Add("ssss", "dddd", DateTime.Now.AddDays(1));
        }
Ejemplo n.º 7
0
        public Client ( MemcachedClient memcachedClient, AspNetMemcachedConfiguration memcachedConfiguration ) {

            SockIOPool pool = SockIOPool.GetInstance();
            List<string> serverList = new List<string>();
            foreach (MemcachedServer server in memcachedConfiguration.MemcachedServerCollection) {
                serverList.Add(server.IP + ":" + server.Port);
            }
            pool.SetServers(serverList.ToArray());

            if (memcachedConfiguration.UseCompression != null && memcachedConfiguration.UseCompression == "yes")
                memcachedClient.EnableCompression = true;
            else
                memcachedClient.EnableCompression = false;

            MemcachedPoolConfig poolConfig = (MemcachedPoolConfig)memcachedConfiguration.PoolConfig;
            pool.InitConnections = (int)poolConfig.InitConnections;
            pool.MinConnections = (int)poolConfig.MinConnections;
            pool.MaxConnections = (int)poolConfig.MaxConnections;
            pool.SocketConnectTimeout = (int)poolConfig.SocketConnectTimeout;
            pool.SocketTimeout = (int)poolConfig.SocketConnect;
            pool.MaintenanceSleep = (int)poolConfig.MaintenanceSleep;
            pool.Failover = (bool)poolConfig.Failover;
            pool.Nagle = (bool)poolConfig.Nagle;
            pool.Initialize();
        }
Ejemplo n.º 8
0
        private String[] serverlist = { "127.0.0.1:11211" }; //要改为配置文件读取

        #endregion Fields

        #region Constructors

        private MemcachedClientSatat(string poolName)
        {
            string memchached = ConfigurationSettings.AppSettings["memchached"];
            string msgnumstr = ConfigurationSettings.AppSettings["msgnum"];
            msgnum = msgnumstr == null ? 0 : int.Parse(msgnumstr);
            string keystr = ConfigurationSettings.AppSettings["analyzedkey"];
            if (keystr != null)
                analyzedkey = keystr;
            serverlist = memchached.Split(',');
            // initialize the pool for memcache servers
            try
            {
                pool = SockIOPool.GetInstance(poolName);
            }
            catch (Exception)
            {

                throw;
            }
            pool.SetServers(serverlist);
            pool.Initialize();
            mc = new MemcachedClient();
            mc.PoolName = poolName;
            mc.EnableCompression = false;
        }
Ejemplo n.º 9
0
        private void btnGetValue_Click(object sender, EventArgs e)
        {
            string[] serverlist = { txtServer.Text };

            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;

            string value = (string)mc.Get(txtKey.Text);
            MessageBox.Show(value);
        }
Ejemplo n.º 10
0
        public static void Main(string[] args)
        {
            //String[] serverlist = { "140.192.34.72:11211", "140.192.34.73:11211"  };
            String[] serverlist = { "192.168.111.152: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();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "10.12.11.44: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("keyddd", "sssssssss");

            mc.Add("ssss", "dddd", DateTime.Now.AddDays(1));

            //mc.Delete()
            //mc.Set()
        }
Ejemplo n.º 12
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;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Declare the private constructor which prevent others construct its instance.
        /// </summary>
        private NHibernateSecondLevelCacheManager()
        {
            // factory = NHibernateSessionManager.Instance.GetSession().GetSessionImplementation().Factory;

            _provider = new MemCacheProvider();
            _provider.Start(new Dictionary<string, string>());

            _client = new MemcachedClient() { PoolName = "nhibernate" };
        }
Ejemplo n.º 14
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            MemcachedClient mc = new MemcachedClient();
            mc.EnableCompression = false;

            mc.Set(txtkey.Text, txtValue.Text);

            SockIOPool.GetInstance().Shutdown();
        }
Ejemplo n.º 15
0
 private static void ReadData(MemcachedClient mc)
 {
     for (int i = 0; i < _loopFor; i++)
     {
         Thread.Sleep(1000);
         Console.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " Contents of cache: " +
                             mc.Get("1") + " @ " + DateTime.Now);
     }
 }
Ejemplo n.º 16
0
		public MemCacheClient(string regionName, IDictionary<string, string> properties)
		{
			region = regionName;
			client = new MemcachedClient {PoolName = PoolName};
			expiry = 300;

			if (properties != null)
			{
				if (properties.ContainsKey("compression_enabled"))
				{
					client.EnableCompression = Convert.ToBoolean(properties["compression_enabled"]);
					if (log.IsDebugEnabled)
					{
						log.DebugFormat("compression_enabled set to {0}", client.EnableCompression);
					}
				}

				var expirationString = GetExpirationString(properties);
				if (expirationString != null)
				{
					expiry = Convert.ToInt32(expirationString);
					if (log.IsDebugEnabled)
					{
						log.DebugFormat("using expiration of {0} seconds", expiry);
					}
				}

				if (properties.ContainsKey("regionPrefix"))
				{
					regionPrefix = properties["regionPrefix"];

					if (log.IsDebugEnabled)
					{
						log.DebugFormat("new regionPrefix :{0}", regionPrefix);
					}
				}
				else
				{
					if (log.IsDebugEnabled)
					{
						log.Debug("no regionPrefix value given, using defaults");
					}
				}

                if (properties.ContainsKey("lingering_delete_disabled"))
                {
                    noLingeringDelete = Convert.ToBoolean(properties["lingering_delete_disabled"]);
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("lingering_delete_disabled set to {0}", noLingeringDelete);
                    }
                }
			}
		}
        public override void Remove(string key)
        {
            InicializaMemcached();

            MemcachedClient cache = new MemcachedClient();

            string chave = MD5(key);

            cache.Delete(chave);

            return;
        }
        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            utcExpiry = TimeZoneInfo.ConvertTimeFromUtc(utcExpiry, TimeZoneInfo.Local);
            var agora = DateTime.Now;
            Debug.WriteLine("Set" + key + " __ " + utcExpiry.Subtract(agora).TotalSeconds.ToString());
            InicializaMemcached();

            MemcachedClient cache = new MemcachedClient();

            string chave = MD5(key);

            cache.Set(chave, entry, utcExpiry);
            return;
        }
Ejemplo n.º 19
0
 protected void btnStats_Click(object sender, EventArgs e)
 {
     // Get the Statistics of memcahed server
     MemcachedClient mc = new MemcachedClient();
     IDictionary stats = mc.Stats();
     foreach (string key1 in stats.Keys)
     {
         lblStats.Text += key1;
         Hashtable values = (Hashtable)stats[key1];
         foreach (string key2 in values.Keys)
         {
             lblStats.Text += key2 + ":" + values[key2] + "\r\n";
         }
         lblStats.Text += "\r\n";
     }
 }
Ejemplo n.º 20
0
 private MemcachedClientSatat(String[] servers)
 {
     string msgnumstr = ConfigurationSettings.AppSettings["msgnum"];
     msgnum = msgnumstr == null ? 0 : int.Parse(msgnumstr);
     string keystr = ConfigurationSettings.AppSettings["analyzedkey"];
     if (keystr != null)
         analyzedkey = keystr;
     this.serverlist = servers;
     // initialize the pool for memcache servers
     pool = SockIOPool.GetInstance();
     pool.SetServers(serverlist);
     pool.Initialize();
     mc = new MemcachedClient();
     //mc.PoolName = poolName;
     mc.EnableCompression = false;
 }
Ejemplo n.º 21
0
        /// <summary>
        /// 创建Memcache服务
        /// </summary>
        /// <param name="serverlist">IP端口列表</param>
        /// <param name="poolName">Socket连接池名称</param>
        /// <returns>Memcache客户端代理类</returns>
        public static MemcachedClient CreateServer(ArrayList serverlist, string poolName)
        {
            //初始化memcache服务器池
            SockIOPool pool = SockIOPool.GetInstance(poolName);
            //设置Memcache池连接点服务器端。
            pool.SetServers(serverlist);
            pool.Initialize();
            //其他参数根据需要进行配置

            //创建了一个Memcache客户端的代理类。
            MemcachedClient mc = new MemcachedClient();
            mc.PoolName = poolName;
            mc.EnableCompression = false;//是否压缩

            return mc;
        }
Ejemplo n.º 22
0
 public static string Status(MemcachedClient mc)
 {
     var sb = new StringBuilder("Cachestatus:" + Environment.NewLine);
     IDictionary stats = mc.Stats();
     foreach (string key1 in stats.Keys)
     {
         sb.AppendLine(key1);
         var values = (Hashtable)stats[key1];
         foreach (string key2 in values.Keys)
         {
             sb.AppendLine(key2 + ":" + values[key2]);
         }
         sb.AppendLine();
     }
     return sb.ToString();
 }
Ejemplo n.º 23
0
        static void Main(string[] args)
        {
            //分布Memcachedf服务IP 端口
            string[] servers =
            {
                "192.168.1.105: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;

            string key1   = "key-1";
            string value1 = "value1";

            mc.Add(key1, value1);
            Console.WriteLine(key1 + "   " + mc.Get(key1));

            string key2   = "key5";
            string value2 = "value5";

            mc.Add(key2, value2);
            Console.WriteLine(key2 + "   " + mc.Get(key2));



            //var userInfos = UserInfoService.GetEntities(u => u.UName == "admin" && u.Pwd == "123456" && u.DelFlag == "0").FirstOrDefault();
            //var userinfo ={string username="******",string wd="123456"}
            mc.Add("d4491076-3a6b-4f47-9515-a2bb64e7869c", "11111");
            Console.WriteLine(mc.Get("d4491076-3a6b-4f47-9515-a2bb64e7869c"));
        }
Ejemplo n.º 24
0
        public static bool Initialize()
        {
            try
            {
                string[] serverlist = { "127.0.0.1" };

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

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

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

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

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

                mc = new MemcachedClient();
                mc.EnableCompression = false;

               Loger.Debug(" memcached client Initialized ");
                System.Collections.Hashtable hsStats = new System.Collections.Hashtable(mc.Stats());
                foreach (DictionaryEntry s in hsStats)
                {
                    Loger.Debug(s.Key.ToString());
                    foreach (DictionaryEntry t in (Hashtable)s.Value)
                    {
                        Loger.Debug(t.Key.ToString() + " : " + t.Value.ToString());
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Loger.Debug("MemcachedClient Initialize error : " + ex.ToString());
                return false;
            }
        }
Ejemplo n.º 25
0
    public static bool fn_MemSet(string key,string value)
    {
        SockIOPool pool = SockIOPool.GetInstance("test");
        pool.SetServers(serverlist);
        pool.Initialize();

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

        try
        {
            return mc.Set(key,value);
        }
        catch (System.Exception e)
        {
        }
        return false;
    }
        public override object Get(string key)
        {
            Debug.WriteLine("Get " + key);

            InicializaMemcached();

            MemcachedClient cache = new MemcachedClient();

            string chave = MD5(key);

            if (cache.KeyExists(chave))
            {
                return cache.Get(chave);
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 27
0
    public static string fn_MemGet(string key)
    {
        SockIOPool pool = SockIOPool.GetInstance("test");
        pool.SetServers(serverlist);
        pool.Initialize();

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

        string values = null;
        try
        {
            values = (string)mc.Get(key);
        }
        catch (System.Exception e)
        {
        }
        return values;
    }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            MemcachedClient mc = new MemcachedClient(); //Needs log4net
            SetupMemcache(mc);

            for (int i = 0; i < 5; i++) {

                Console.WriteLine("Adding item: " + i);
                mc.Set("1", DateTime.Now, DateTime.Now.AddSeconds(_loopFor*2));

                Console.WriteLine("Starting");

                //Thread write = new Thread(delegate()
                //                          {
                //                              WriteData(mc);
                //                          }
                //                     );

                //write.Start();

                Thread readA = new Thread(delegate(){ ReadData(mc); });
                readA.Start();

                Thread readB = new Thread(delegate(){ ReadData(mc); });
                readB.Start();

                Thread readC = new Thread(delegate(){ ReadData(mc); });
                readC.Start();

                Console.WriteLine(Status(mc));

                readA.Join();
                readB.Join();
                readC.Join();

                Console.WriteLine();
                Console.WriteLine("Bored now...");
                Console.WriteLine(Status(mc));
            }
            Console.ReadLine();
        }
Ejemplo n.º 29
0
        private MemcachedClient client;             // Memcached客户端

        public MemcachedService(string instanceName, string aMemServers, string aMemWeights)
        {
            this.instanceName = instanceName;
            this.memServers = aMemServers;
            this.memWeights = aMemWeights;

            SockIOPool sockIOPool = SockIOPool.GetInstance(this.instanceName);
            if (!sockIOPool.Initialized)
            {
                lock (syncRoot)
                {
                    if (!sockIOPool.Initialized)
                    {
                        string[] servers;
                        int[] weights;
                        this.GetMemcachedServerConfig(out servers, out weights);

                        sockIOPool.SetServers(servers);             // 设置服务器列表                       
                        sockIOPool.SetWeights(weights);             // 各服务器之间负载均衡的设置

                        //socket pool设置
                        sockIOPool.InitConnections = 16;             // 初始化时创建的连接数
                        sockIOPool.MinConnections = 16;              // 最小连接数
                        sockIOPool.MaxConnections = 256;             // 最大连接数

                        sockIOPool.MaxIdle = 1000 * 60 * 60 * 3;    // 连接的最大空闲时间,下面设置为3个小时(单位ms),超过这个设置时间,连接会被释放掉
                        sockIOPool.SocketTimeout = 1000 * 3;        // 通讯的超时时间,下面设置为3秒(单位ms),.NET版本没有实现
                        sockIOPool.SocketConnectTimeout = 0;        // socket连接的超时时间,下面设置表示连接不超时,即一直保持连接状态
                        sockIOPool.Nagle = false;                   // 是否对TCP/IP通讯使用Nalgle算法,.NET版本没有实现
                        sockIOPool.MaintenanceSleep = 60;           // 维护线程的间隔激活时间,下面设置为60秒(单位s),设置为0表示不启用维护线程
                        sockIOPool.MaxBusy = 1000 * 10;             // socket单次任务的最大时间,超过这个时间socket会被强行中断掉(当前任务失败)

                        sockIOPool.Initialize();
                    }
                }
            }

            this.client = new MemcachedClient();
            this.client.PoolName = this.instanceName;
            this.client.EnableCompression = false;
        }
Ejemplo n.º 30
0
        static CacheHelper()
        {
            string[] servers = { "192.168.1.100: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;
        }
Ejemplo n.º 31
0
        public MemcacheStorage() {
            //分布Memcachedf服务IP 端口
            String[] servers = { "192.168.77.128:11211", "192.168.77.128:11212" };

            //初始化池
            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();

            //实例化一个client
            client = new MemcachedClient();
            client.EnableCompression = false;
        }
Ejemplo n.º 32
0
        static void Main(string[] args)
        {
            //分布Memcached服务IP:端口
            //string[] servers = { "192.168.1.113:11211", "192.168.202.128:11211" };
            string[] servers = { "127.0.0.1:11211" };
            //初始化池。通俗说pool就是与mm服务器端交换数据的对象,通过它设置连接mm服务器端相关属性
            SockIOPool pool = SockIOPool.GetInstance();

            //设置服务器列表
            pool.SetServers(servers);
            //初始化时创建连接数
            pool.InitConnections = 3;
            //最小连接数
            pool.MinConnections = 3;
            //最大连接数
            pool.MaxConnections = 5;
            //socket连接的超时时间,如果为0表示不超时(单位ms),即一直保持链接状态
            pool.SocketConnectTimeout = 1000;
            //通讯的超时时间,下面设置为3秒(单位ms),
            pool.SocketTimeout = 3000;
            //维护线程的间隔激活时间,下面设置为30秒(单位s),设置为0时表示不启用维护线程
            pool.MaintenanceSleep = 30;
            //设置SocktIO池的故障标志
            pool.Failover = true;
            //是否对TCP/IP通讯使用nalgle算法,
            pool.Nagle = false;
            pool.Initialize();
            //客户端实例
            MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();

            //是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被以压缩的形式存储
            mc.EnableCompression = false;
            //mc.Add("keyxdz", "1ssss");//这个时候数据存储到哪里去了呢(某一台机器缓存)?根据mm客户端集群原理可理解
            //mc.Set("keyxdz3", "2ssss", DateTime.Now.AddDays(1));//表示有效期为1天

            mc.Delete("keyxdz3");//删除键为keyxdz的值,之后通过MM的命令提示符窗口也不到key为keyxdz的值了
            string s = mc.Get("keyxdz3").ToString();

            Console.WriteLine(s);//输出2ssss
        }
Ejemplo n.º 33
0
        public BlipMessageServerHandler ( string[] servers, string rootQueueId ) {
            this.rootQueueId = rootQueueId;

            SockIOPool pool = SockIOPool.GetInstance("bucker-queue-server");
            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();

            mc = new MemcachedClient();
            mc.PoolName = "bucker-queue-server";
            mc.EnableCompression = false;

            mc.Add(rootQueueId, String.Empty);
        }
        public override object Add(string key, object entry, DateTime utcExpiry)
        {
            utcExpiry = TimeZoneInfo.ConvertTimeFromUtc(utcExpiry, TimeZoneInfo.Local);
            var agora = DateTime.Now;
            Debug.WriteLine("Add"+key + " __ " + utcExpiry.Subtract(agora).TotalSeconds.ToString());

            InicializaMemcached();

            MemcachedClient cache = new MemcachedClient();

            string chave = MD5(key);

            if (cache.KeyExists(chave))
            {
                return cache.Get(chave);
            }
            else
            {
                cache.Set(chave, entry, utcExpiry.ToUniversalTime());
                return entry;
            }
        }
Ejemplo n.º 35
0
        public void clear()
        {
            string[] servers = { "192.168.1.113: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.Delete("cache");
            mc.Delete("endCache");
            Response.Write("清空缓存成功");
        }
Ejemplo n.º 36
0
        static MemCacheHelper()
        {
            //分布Memcached服务IP 端口
            string[] servers = ConfigurationManager.AppSettings["MemcachedServers"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            //初始化池
            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();

            //客户端实例
            mc = new Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression = false;
        }
Ejemplo n.º 37
0
        public CacheManager(params string[] hostring)
        {
            string poolName = Guid.NewGuid().ToString("N");
            mPool = SockIOPool.GetInstance(poolName);
            mPool.SetServers(hostring);

            mPool.InitConnections = 5;
            mPool.MinConnections = 5;
            mPool.MaxConnections = 10;

            mPool.SocketConnectTimeout = 1000;
            mPool.SocketTimeout = 3000;

            mPool.MaintenanceSleep = 30;
            mPool.Failover = true;

            mPool.Nagle = false;
            mPool.Initialize();
            mCacheClient = new MemcachedClient();
            mCacheClient.PoolName = poolName;
            mCacheClient.EnableCompression = false;
        }
Ejemplo n.º 38
0
        public MemcacheWriter()
        {
            string[] servers = { "127.0.0.1:11211" };//默认为本机作为服务器
            string   strAppMemcachedServer = ConfigurationManager.AppSettings["MemcacheServerList"];

            if (strAppMemcachedServer != null)//说明web.config进行了配置
            {
                servers = strAppMemcachedServer.Split(',');
            }
            //初始化池。通俗说pool就是与mm服务器端交换数据的对象,通过它设置连接mm服务器端相关属性
            SockIOPool pool = SockIOPool.GetInstance();

            //设置服务器列表
            pool.SetServers(servers);
            //初始化时创建连接数
            pool.InitConnections = 3;
            //最小连接数
            pool.MinConnections = 3;
            //最大连接数
            pool.MaxConnections = 5;
            //socket连接的超时时间,如果为0表示不超时(单位ms),即一直保持链接状态
            pool.SocketConnectTimeout = 1000;
            //通讯的超时时间,下面设置为3秒(单位ms),
            pool.SocketTimeout = 3000;
            //维护线程的间隔激活时间,下面设置为30秒(单位s),设置为0时表示不启用维护线程
            pool.MaintenanceSleep = 30;
            //设置SocktIO池的故障标志
            pool.Failover = true;
            //是否对TCP/IP通讯使用nalgle算法,
            pool.Nagle = false;
            pool.Initialize();
            //客户端实例
            MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();

            //是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被储存在压缩的形式
            mc.EnableCompression = false;
            memcachedClient      = mc;
        }
Ejemplo n.º 39
0
        public MemCacheWriter()
        {
            //string[] servers = { "192.168.1.109:11211" };
            string[]   servers = System.Configuration.ConfigurationManager.AppSettings["MencacheServerList"].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
            {
                EnableCompression = false
            };

            memcachedClient = mc;
        }
Ejemplo n.º 40
0
        static void Main(string[] args)
        {
            string[] servers = { "127.0.0.1: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("gct", "aiyangying");
            //mc.Delete("gct");
            mc.Add("smj", "hhh", DateTime.Now.AddDays(1));
        }
Ejemplo n.º 41
0
        public void test()
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "127.0.0.1:11211" };

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

            pool.SetServers(servers, true);
            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;
            StringBuilder sb = new StringBuilder();

            //写入缓存
            sb.AppendLine("写入缓存测试:");
            sb.AppendLine("<br>_______________________________________<br>");
            if (mc.KeyExists("cache"))
            {
                sb.AppendLine("缓存cache已存在");
            }
            else
            {
                mc.Set("cache", "写入缓存时间:" + DateTime.Now.ToString());
                sb.AppendLine("缓存已成功写入到cache");
            }

            sb.AppendLine("<br>_______________________________________<br>");
            sb.AppendLine("读取缓存内容如下:<br>");
            sb.AppendLine(mc.Get("cache").ToString());

            //测试缓存过期
            sb.AppendLine("<br>_______________________________________<br>");
            if (mc.KeyExists("endCache"))
            {
                sb.AppendLine("缓存endCache已存在,过期时间为:" + mc.Get("endCache").ToString());
            }
            else
            {
                mc.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1));
                sb.AppendLine("缓存已更新写入到endCache,写入时间:" + DateTime.Now.ToString() + " 过期时间:" + DateTime.Now.AddMinutes(1).ToString());
            }

            //分析缓存状态
            Hashtable ht = mc.Stats();

            sb.AppendLine("<br>_______________________________________<br>");
            sb.AppendLine("Memcached Stats:");
            sb.AppendLine("<br>_______________________________________<br>");
            foreach (DictionaryEntry de in ht)
            {
                Hashtable info = (Hashtable)de.Value;
                foreach (DictionaryEntry de2 in info)
                {
                    sb.AppendLine(de2.Key.ToString() + ":&nbsp;&nbsp;&nbsp;&nbsp;" + de2.Value.ToString() + "<br>");
                }
            }
            Console.WriteLine(sb.ToString());
            //Response.Write(sb.ToString());
        }