• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#MemCached分布式缓存

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;

using System.Text;

using Memcached.ClientLibrary;
using System.Diagnostics;

namespace TestMemcachedApp
{
    class Program
    {
        [STAThread]
        public static void Main(String[] args)
        {


            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start(); //  开始监视代码运行时间

            string[] serverlist = { "192.168.0.107:11211" };//服务器可以是多个

            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();
            pool.SetServers(serverlist);//设置连接池可用的cache服务器列表,server的构成形式是IP:PORT(如:127.0.0.1:11211)
            pool.InitConnections = 3;//初始连接数
            pool.MinConnections = 3;//最小连接数
            pool.MaxConnections = 5;//最大连接数
            pool.SocketConnectTimeout = 1000;//设置连接的套接字超时
            pool.SocketTimeout = 3000;//设置套接字超时读取
            pool.MaintenanceSleep = 30;//设置维护线程运行的睡眠时间。如果设置为0,那么维护线程将不会启动,30就是每隔30秒醒来一次

            //获取或设置池的故障标志。
            //如果这个标志被设置为true则socket连接失败,将试图从另一台服务器返回一个套接字如果存在的话。
            //如果设置为false,则得到一个套接字如果存在的话。否则返回NULL,如果它无法连接到请求的服务器。
            pool.Failover = true;

            pool.Nagle = false;//如果为false,对所有创建的套接字关闭Nagle的算法
            pool.Initialize();

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




            for (int i = 1; i <= 50000; i++)

            {
                mc.Set(Guid.NewGuid().ToString(), "sadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahlsadsadsakjdklasjfklsjfalkshkfjhdskjfhskjfhkjhjsdkfhkjsahl;kfjsakldm,sa.mf,sdmnm,xnkvlhjxkclvnklsjfkldsjnfksdnfklsdnfm,dsnfm,sdnfdshfkjdshfkjdshkfjhdskjfsdfsdfsdf" + i.ToString());
                // mc.Delete(i.ToString()); 
            }


                //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");
                //}


                //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");
                //}
              

            SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets


            //  you code ....
            stopwatch.Stop(); //  停止监视
            TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
            double hours = timespan.TotalHours; // 总小时
            double minutes = timespan.TotalMinutes;  // 总分钟
            double seconds = timespan.TotalSeconds;  //  总秒数
            double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数
            Console.WriteLine(seconds.ToString()+"用时");
            Console.ReadLine();
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap