Memched还是比较简单的,这里把C#的相关操作整理了一下,Mark~
1 /// <summary> 2 /// 缓存操作类。 3 /// </summary> 4 /// <remarks> 5 /// 简介: 6 /// Memcached是一个开源的高性能分布式缓存系统,基于key-value的形式存储,简洁强大,开发速度快。 7 /// 安装教程:http://www.runoob.com/memcached/memcached-install.html 8 /// 使用类似:/usr/bin/memcached -p 11211 -u root -d 启动 9 /// 存储命令: 10 /// Memcached真的很简单,set、add、replace、append、prepend、cas命令的参数也基本相同,这里就一一缀诉,以set为例, 11 /// set key flags exptime bytes [noreply] 12 /// value 13 /// key:键值对key-value中的key; 14 /// flags:存储键值对的额外信息; 15 /// exptime:过期时间以秒为单位,0表示永远 16 /// bytes:在缓存中存储的字节数 17 /// noreply(可选):告知服务器不需要返回数据 18 /// value:存储的值(始终位于第二行) 19 /// 其他命令: 20 /// get key:查找命令,多值的查找请使用get key1 key2 key3 21 /// delete key:删除命令 22 /// stats:返回统计信息 23 /// flush all:用于清理缓存中的所有的key 24 /// </remarks> 25 public class MemcachedCore 26 { 27 public MemcachedClient _mClient; 28 29 /// <summary> 30 /// 初始化。 31 /// </summary> 32 /// <param name="connStr">链接字符串例:127.0.0.1:11211</param> 33 /// <param name="poolName"></param> 34 public MemcachedCore(string connStr, string poolName = "") 35 { 36 _mClient = new MemcachedClient(); 37 _mClient.PoolName = poolName; 38 _mClient.EnableCompression = false; 39 40 //初始化 41 SockIOPool pool; //memcache的pool可以关联多个server,虽然Memcached是分布式的,但是本身是不支持的,需要在客户端编写分布式策略,通过weight来实现 42 if (string.IsNullOrEmpty(poolName)) 43 pool = SockIOPool.GetInstance(); 44 else 45 pool = SockIOPool.GetInstance(_mClient.PoolName); 46 pool.SetServers(new string[] { connStr }); 47 pool.Nagle = false; 48 pool.Initialize(); 49 } 50 51 public void Set<T>(string key, T obj) 52 { 53 _mClient.Set(key, obj); 54 } 55 56 public void Set<T>(string key, T obj, DateTime expireTime) 57 { 58 _mClient.Set(key, obj, expireTime); 59 } 60 61 public void Add<T>(string key, T obj) 62 { 63 _mClient.Add(key, obj); 64 } 65 66 public void Add<T>(string key, T obj, DateTime expireTime) 67 { 68 _mClient.Add(key, obj, expireTime); 69 } 70 71 public void Update<T>(string key, T obj) 72 { 73 _mClient.Replace(key, obj); 74 } 75 76 public void Update<T>(string key, T obj, DateTime expireTime) 77 { 78 _mClient.Replace(key, obj, expireTime); 79 } 80 81 public void Delete(string key) 82 { 83 _mClient.Delete(key); 84 } 85 86 public T Get<T>(string key) 87 { 88 return (T)_mClient.Get(key); 89 } 90 91 public List<T> GetList<T>(string[] keys) 92 { 93 return _mClient.GetMultipleArray(keys).Select(p => (T)p).ToList(); 94 } 95 }