Posted on 2018-04-16 07:29
viery 閱讀(116)
評論(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