1 using Memcached.ClientLibrary; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace Common 9 { 10 public class MemcacheHelper 11 { 12 private static readonly MemcachedClient mc = null; 13 14 static MemcacheHelper() 15 { 16 //最好放在配置文件中 17 string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" }; 18 19 //初始化池 20 SockIOPool pool = SockIOPool.GetInstance(); 21 pool.SetServers(serverlist); 22 23 pool.InitConnections = 3; 24 pool.MinConnections = 3; 25 pool.MaxConnections = 5; 26 27 pool.SocketConnectTimeout = 1000; 28 pool.SocketTimeout = 3000; 29 30 pool.MaintenanceSleep = 30; 31 pool.Failover = true; 32 33 pool.Nagle = false; 34 pool.Initialize(); 35 36 // 获得客户端实例 37 mc = new MemcachedClient(); 38 mc.EnableCompression = false; 39 } 40 /// <summary> 41 /// 存储数据 42 /// </summary> 43 /// <param name="key"></param> 44 /// <param name="value"></param> 45 /// <returns></returns> 46 public static bool Set(string key,object value) 47 { 48 return mc.Set(key, value); 49 } 50 public static bool Set(string key, object value,DateTime time) 51 { 52 return mc.Set(key, value,time); 53 } 54 /// <summary> 55 /// 获取数据 56 /// </summary> 57 /// <param name="key"></param> 58 /// <returns></returns> 59 public static object Get(string key) 60 { 61 return mc.Get(key); 62 } 63 /// <summary> 64 /// 删除 65 /// </summary> 66 /// <param name="key"></param> 67 /// <returns></returns> 68 public static bool Delete(string key) 69 { 70 if (mc.KeyExists(key)) 71 { 72 return mc.Delete(key); 73 74 } 75 return false; 76 77 } 78 } 79 }
时间: 2024-10-08 20:04:34