标签:执行 分享图片 取值 sock dcl sleep cli shc 项目
基本概念:MemCache是项目的名称, MemCached是MemCache服务器端可以执行文件的名称
1 public static class MemcheHelper 2 { 3 static readonly MemcachedClient mc=null; 4 static MemcheHelper() 5 { 6 //最好放在配置文件中 7 string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" }; 8 9 //初始化池 10 SockIOPool pool = SockIOPool.GetInstance(); 11 pool.SetServers(serverlist); 12 pool.InitConnections = 3; 13 pool.MinConnections = 3; 14 pool.MaxConnections = 5; 15 pool.SocketConnectTimeout = 1000; 16 pool.SocketTimeout = 3000; 17 pool.MaintenanceSleep = 30; 18 pool.Failover = true; 19 pool.Nagle = false; 20 pool.Initialize(); 21 22 // 获得客户端实例 23 mc = new MemcachedClient(); 24 mc.EnableCompression = false; 25 } 26 /// <summary> 27 /// 这是添加的方法 28 /// </summary> 29 /// <param name="key"></param> 30 /// <param name="value"></param> 31 /// <returns></returns> 32 public static bool Set(string key,object value) 33 { 34 return mc.Set(key, value); 35 } 36 public static bool Set(string key, object value,DateTime time)//指定过期时间,最大30天 37 { 38 return mc.Set(key, value,time); 39 40 } 41 /// <summary> 42 /// 获取数据 43 /// </summary> 44 /// <param name="key"></param> 45 /// <returns></returns> 46 public static object Get(string key) 47 { 48 return mc.Get(key); 49 } 50 51 /// <summary> 52 /// 删除数据 53 /// </summary> 54 /// <param name="key"></param> 55 /// <returns></returns> 56 public static bool Delete(string key) 57 { 58 if (mc.KeyExists(key)) 59 { 60 return mc.Delete(key); 61 } 62 return false; 63 } 64 }
代码中简单使用
static void Main(string[] args) { //添加,保存一天 MemcheHelper.Set("TestKey", "TestValue",DateTime.Now.AddDays(1)); //取值 MemcheHelper.Get("TestKey"); //删除 MemcheHelper.Delete("TestKey"); }
标签:执行 分享图片 取值 sock dcl sleep cli shc 项目
原文地址:https://www.cnblogs.com/wyy1234/p/9029639.html