Example #1
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();
        }
Example #2
0
 public static void Add(string key, object value, DateTime ExpireDate)
 {
     if (mc.KeyExists(key))
     {
         mc.Set(key, value, ExpireDate);
     }
     else
     {
         mc.Add(key, value, ExpireDate);
     }
 }
        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;
        }
Example #4
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;
    }
Example #5
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();
        }
        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;
            }
        }
Example #7
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());
        }
Example #8
0
        public static void Main(String[] args)
        {
            int runs = 100;
            int start = 200;
            if(args.Length > 1)
            {
                runs = int.Parse(args[0]);
                start = int.Parse(args[1]);
            }

            string[] serverlist = { "192.168.111.16:11211", "192.168.111.152:11211" };

            // initialize the pool for memcache servers
            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();

            // initialize the pool for memcache servers
            //			SockIOPool pool = SockIOPool.Instance;
            //			pool.Servers = serverlist;
            //
            //			pool.InitConn = 5;
            //			pool.MinConn = 5;
            //			pool.MaxConn = 50;
            //			pool.MaintSleep = 30;
            //			pool.SocketTO = 1000;
            //
            //			pool.Nagle = false;
            //			pool.Initialize();

            //
            //			// get client instance
            MemcachedClient mc = new MemcachedClient();
            mc.EnableCompression = false;

            //			MemcachedClient mc = new MemcachedClient();
            //			mc.CompressEnable = false;
            //			mc.CompressThreshold = 0;
            //			mc.Serialize = true;

            string keyBase = "testKey";
            string obj = "This is a test of an object blah blah es, serialization does not seem to slow things down so much.  The gzip compression is horrible horrible performance, so we only use it for very large objects.  I have not done any heavy benchmarking recently";

            long begin = DateTime.Now.Ticks;
            for(int i = start; i < start+runs; i++)
            {
                mc.Set(keyBase + i, obj);
            }
            long end = DateTime.Now.Ticks;
            long time = end - begin;

            Console.WriteLine(runs + " sets: " + new TimeSpan(time).ToString() + "ms");

            begin = DateTime.Now.Ticks;
            int hits = 0;
            int misses = 0;
            for(int i = start; i < start+runs; i++)
            {
                string str = (string) mc.Get(keyBase + i);
                if(str != null)
                    ++hits;
                else
                    ++misses;
            }
            end = DateTime.Now.Ticks;
            time = end - begin;

            Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms");
            Console.WriteLine("Cache hits: " + hits.ToString());
            Console.WriteLine("Cache misses: " + misses.ToString());

            IDictionary stats = mc.Stats();
            foreach(string key1 in stats.Keys)
            {
                Console.WriteLine(key1);
                Hashtable values = (Hashtable)stats[key1];
                foreach(string key2 in values.Keys)
                {
                    Console.WriteLine(key2 + ":" + values[key2]);
                }
                Console.WriteLine();
            }

            SockIOPool.GetInstance().Shutdown();
        }
        public void VerifyTheWholeLifecycleOfARunningPool()
        {
            StartPool();
            Assert.IsTrue(pool.IsRunning);

            MemcachedClient client = new MemcachedClient();
            client.PoolName = pool.PoolName;
            client.Set("key", "value");
            Assert.AreEqual("value", client.Get("key"));

            pool.Stop();
            Assert.IsFalse(pool.IsRunning);
        }
Example #10
0
 private static void WriteData(MemcachedClient mc)
 {
     for (int i = 0; i < _loopFor; i++)
     {
         Thread.Sleep(1000);
         if (mc.Get("1") == null)
             mc.Set("1", DateTime.Now, DateTime.Now.AddSeconds(10));
     }
 }
        public void test()
        {
            //分布Memcachedf服务IP 端口
            string[] servers = { "192.168.3.33: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;
            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>");
                }
            }
            Response.Write(sb.ToString());
        }