Posted on 2018-04-16 07:29 
viery 阅读(124) 
评论(0)  编辑  收藏  
			
			
		 
		

 1 using Memcached.ClientLibrary;
 2 using System;
 3 using System.Collections;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace MemCachedDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             //分布Memcachedf服务IP 端口
16             string[] servers = { "127.0.0.1:11211"};
17 
18             //初始化池
19             SockIOPool pool = SockIOPool.GetInstance();
20             pool.SetServers(servers);
21             pool.InitConnections = 3;
22             pool.MinConnections = 3;
23             pool.MaxConnections = 5;
24             pool.SocketConnectTimeout = 1000;
25             pool.SocketTimeout = 3000;
26             pool.MaintenanceSleep = 30;
27             pool.Failover = true;
28             pool.Nagle = false;
29             pool.Initialize();
30             //客户端实例
31             MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();
32             mc.EnableCompression = false;
33             //写入缓存
34          
35             Console.WriteLine("写入缓存测试:");
36             Console.WriteLine("_______________________________________");
37             if (mc.KeyExists("cache"))
38             {
39                 Console.WriteLine("缓存cache已存在");
40             }
41             else
42             {
43                 mc.Set("cache", "Time is:" + DateTime.Now.ToString());
44                 Console.WriteLine("缓存已成功写入到cache");
45             }
46             Console.WriteLine("_______________________________________");
47             Console.WriteLine("读取缓存内容如下:");
48             Console.WriteLine(mc.Get("cache").ToString());
49 
50             //测试缓存过期
51             Console.WriteLine("_______________________________________");
52             if (mc.KeyExists("endCache"))
53             {
54                 Console.WriteLine("缓存endCache已存在,过期时间为:" + mc.Get("endCache").ToString());
55             }
56             else
57             {
58                 mc.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1));
59                 Console.WriteLine("缓存已更新写入到endCache,写入时间:" + DateTime.Now.ToString() + " 过期时间:" + DateTime.Now.AddMinutes(1).ToString());
60             }
61 
62             //分析缓存状态
63             Hashtable ht = mc.Stats();
64             Console.WriteLine("_______________________________________");
65             Console.WriteLine("Memcached Stats:");
66             Console.WriteLine("_______________________________________");
67             foreach (DictionaryEntry de in ht)
68             {
69                 Hashtable info = (Hashtable)de.Value;
70                 foreach (DictionaryEntry de2 in info)
71                 {
72                     Console.WriteLine(de2.Key.ToString() + ":" + de2.Value.ToString() + "");
73                 }
74             }
75            
76             Console.Read();
77         }
78     }
79 }
80