Memcached的.NET客户端(二)【BeITMemcached使用】

创建控制台应用程序,Program.cs代码如下:

 class Program
    {
        public static void Main(string[] args)
        {
            //---------------------
            // 设置客户端
            //---------------------
            Console.WriteLine("设置Memcached的客户端");
            MemcachedClient.Setup("MyCache", new[] {"localhost"});

            //它可以有几个客户不同的配置:
            //如果它是不能解析的主机,这个方法会抛出异常。
            try
            {
                MemcachedClient.Setup("MyOtherCache", new[] {"server1.example.com:12345", "server2.example.com:12345"});
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            //获取我们刚建立的实例,所以我们可以使用它。您可以存储此引用,每次你需要它的时候把它拿来,真的没关系
            MemcachedClient cache = MemcachedClient.GetInstance("MyCache");

            //也可以设置客户标准配置文件。检查这个项目App.config文件“beitmemcached”一节,你会看到一个名为“MyConfigFileCache”客户端的定义
            MemcachedClient configFileCache = MemcachedClient.GetInstance("MyConfigFileCache");

            //更改客户端设置
            cache.SendReceiveTimeout = 5000;
            cache.ConnectTimeout = 5000;
            cache.MinPoolSize = 1;
            cache.MaxPoolSize = 5;

            //----------------
            // 使用客户端
            //----------------

            //设置一些项目
            Console.WriteLine("存放一些items.");
            cache.Set("mystring", "The quick brown fox jumped over the lazy dog.");
            cache.Set("myarray", new[] {"这是第一个字符串.", "这是第二个字符串."});
            cache.Set("myinteger", 4711);
            cache.Set("mydate", new DateTime(2008, 02, 23));
            //使用自定义哈希
            cache.Set("secondstring", "qqqqqqqqqqqqqqqqqqqqqq", 4711);

            //得到一个字符串
            var str = cache.Get("mystring") as string;
            if (str != null)
            {
                Console.WriteLine("取数据 根据键: mystring, 值: " + str);
            }

            //得到一个对象
            var array = cache.Get("myarray") as string[];
            if (array != null)
            {
                Console.WriteLine("取数据 根据键: myarray, value 1: " + array[0] + ", value 2: " + array[1]);
            }

            //一次获取多个值
            object[] result = cache.Get(new[] {"myinteger", "mydate"});
            if (result[0] != null && result[0] is int)
            {
                Console.WriteLine("取数据 根据键: myinteger, value: " + (int) result[0]);
            }
            if (result[1] != null && result[1] is DateTime)
            {
                Console.WriteLine("取数据 根据键: mydate, value: " + (DateTime) result[1]);
            }

            str = cache.Get("secondstring", 4711) as string;
            if (str != null)
            {
                Console.WriteLine("取数据 根据键和 custom hash: secondstring, value: " + str);
            }

            //设置一个计数器
            Console.WriteLine("设置一个数据项来递增和递减.");
            cache.SetCounter("mycounter", 9000);
            ulong? counter = cache.GetCounter("mycounter");
            if (counter.HasValue)
            {
                Console.WriteLine("取得 mycounter, 值为: " + counter.Value);
            }

            //增加计数器
            counter = cache.Increment("mycounter", 1);
            if (counter.HasValue)
            {
                Console.WriteLine("1开始递增计数器, 新值: " + counter.Value);
            }

            //递减计数器
            counter = cache.Decrement("mycounter", 9000);
            if (counter.HasValue)
            {
                Console.WriteLine("9001开始减去9000, 新值: " + counter.Value);
            }

            //追加和预先准备
            Console.WriteLine("追加和预先准备");
            cache.Set("foo", "bar");
            Console.WriteLine("追加 baz");
            cache.Append("foo", " baz");
            Console.WriteLine("前面加上 foo");
            cache.Prepend("foo", "foo ");
            Console.WriteLine("新值: " + cache.Get("foo"));

            //CAS(Check and Set)协议,处理同一item被多个线程更改过程的并发问题
            cache.Delete("castest");
            Console.WriteLine("Trying to CAS不存在的键castest: " + cache.CheckAndSet("castest", "a", 0));
            Console.WriteLine("设定键: castest, 值: a");
            cache.Set("castest", "a");
            Console.WriteLine("Trying to CAS键castest 用错误的唯一标识号: " + cache.CheckAndSet("castest", "a", 0));
            ulong unique;
            cache.Gets("castest", out unique);
            Console.WriteLine("获取castest键的cas唯一标识号为: " + unique);
            Console.WriteLine("Trying to CAS 再次用上面的唯一标识号: " + cache.CheckAndSet("castest", "b", unique));
            var value = cache.Gets("castest", out unique) as string;
            Console.WriteLine("新值: " + value + ", 新的唯一标识号: " + unique);

            Console.WriteLine("显示套接字池状态:");
            foreach (var host in cache.Status())
            {
                Console.WriteLine("Host: " + host.Key);
                foreach (var item in host.Value)
                {
                    Console.WriteLine("\t" + item.Key + ": " + item.Value);
                }
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("完成,按Enter键退出.");

            Console.ReadLine();
        }
    }
结果如图:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值