示例#1
0
        /// <summary>
        /// 查看缓存的相关状态参数
        /// </summary>
        /// <returns></returns>
        public static Hashtable GetCacheStats()
        {
            ArrayList al = new ArrayList();

            string[] serverlist = Helper.GetAppSettings("MemcachedServerList").Trim().Split(',');
            if (serverlist.Count <string>() > 1)
            {
                foreach (string i in serverlist)
                {
                    al.Add(i);
                }
                return(MC.Stats(al));
            }
            else
            {
                return(MC.Stats());
            }
        }
示例#2
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();
 }
示例#3
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";
     }
 }
示例#4
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;
            }
        }
示例#5
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());
        }
示例#6
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 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());
        }