时间:2021-07-01 10:21:17 帮助过:10人阅读
/// <summary> /// redis配置文件信息 /// 也可以放到配置文件去 /// </summary> public sealed class RedisConfigInfo { /// <summary> /// 可写的Redis链接地址 /// format:ip1,ip2 /// /// 默认6379端口 /// </summary> public string WriteServerList = "127.0.0.1:6379"; /// <summary> /// 可读的Redis链接地址 /// format:ip1,ip2 /// </summary> public string ReadServerList = "127.0.0.1:6379"; /// <summary> /// 最大写链接数 /// </summary> public int MaxWritePoolSize = 60; /// <summary> /// 最大读链接数 /// </summary> public int MaxReadPoolSize = 60; /// <summary> /// 本地缓存到期时间,单位:秒 /// </summary> public int LocalCacheTime = 180; /// <summary> /// 自动重启 /// </summary> public bool AutoStart = true; /// <summary> /// 是否记录日志,该设置仅用于排查redis运行时出现的问题, /// 如redis工作正常,请关闭该项 /// </summary> public bool RecordeLog = false; }View Code
1 /// <summary> 2 /// RedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存 3 /// </summary> 4 public abstract class RedisBase : IDisposable 5 { 6 public IRedisClient iClient { get; private set; } 7 /// <summary> 8 /// 构造时完成链接的打开 9 /// </summary> 10 public RedisBase() 11 { 12 iClient = RedisManager.GetClient(); 13 } 14 15 //public static IRedisClient iClient { get; private set; } 16 //static RedisBase() 17 //{ 18 // iClient = RedisManager.GetClient(); 19 //} 20 21 22 private bool _disposed = false; 23 protected virtual void Dispose(bool disposing) 24 { 25 if (!this._disposed) 26 { 27 if (disposing) 28 { 29 iClient.Dispose(); 30 iClient = null; 31 } 32 } 33 this._disposed = true; 34 } 35 public void Dispose() 36 { 37 Dispose(true); 38 GC.SuppressFinalize(this); 39 } 40 41 public void Transcation() 42 { 43 using (IRedisTransaction irt = this.iClient.CreateTransaction()) 44 { 45 try 46 { 47 irt.QueueCommand(r => r.Set("key", 20)); 48 irt.QueueCommand(r => r.Increment("key", 1)); 49 irt.Commit(); // 提交事务 50 } 51 catch (Exception ex) 52 { 53 irt.Rollback(); 54 throw ex; 55 } 56 } 57 } 58 59 60 /// <summary> 61 /// 清除全部数据 请小心 62 /// </summary> 63 public virtual void FlushAll() 64 { 65 iClient.FlushAll(); 66 } 67 68 /// <summary> 69 /// 保存数据DB文件到硬盘 70 /// </summary> 71 public void Save() 72 { 73 iClient.Save();//阻塞式save 74 } 75 76 /// <summary> 77 /// 异步保存数据DB文件到硬盘 78 /// </summary> 79 public void SaveAsync() 80 { 81 iClient.SaveAsync();//异步save 82 } 83 }View Code
/// <summary> /// key-value 键值对:value可以是序列化的数据 /// </summary> public class RedisStringService : RedisBase { #region 赋值 /// <summary> /// 设置key的value /// </summary> public bool Set<T>(string key, T value) { return base.iClient.Set<T>(key, value); } /// <summary> /// 设置key的value并设置过期时间 /// </summary> public bool Set<T>(string key, T value, DateTime dt) { return base.iClient.Set<T>(key, value, dt); } /// <summary> /// 设置key的value并设置过期时间 /// </summary> public bool Set<T>(string key, T value, TimeSpan sp) { return base.iClient.Set<T>(key, value, sp); } /// <summary> /// 设置多个key/value /// </summary> public void Set(Dictionary<string, string> dic) { base.iClient.SetAll(dic); } #endregion #region 追加 /// <summary> /// 在原有key的value值之后追加value,没有就新增一项 /// </summary> public long Append(string key, string value) { return base.iClient.AppendToValue(key, value); } #endregion #region 获取值 /// <summary> /// 获取key的value值 /// </summary> public string Get(string key) { return base.iClient.GetValue(key); } /// <summary> /// 获取多个key的value值 /// </summary> public List<string> Get(List<string> keys) { return base.iClient.GetValues(keys); } /// <summary> /// 获取多个key的value值 /// </summary> public List<T> Get<T>(List<string> keys) { return base.iClient.GetValues<T>(keys); } #endregion #region 获取旧值赋上新值 /// <summary> /// 获取旧值赋上新值 /// </summary> public string GetAndSetValue(string key, string value) { return base.iClient.GetAndSetValue(key, value); } #endregion #region 辅助方法 /// <summary> /// 获取值的长度 /// </summary> public long GetLength(string key) { return base.iClient.GetStringCount(key); } /// <summary> /// 自增1,返回自增后的值 /// </summary> public long Incr(string key) { return base.iClient.IncrementValue(key); } /// <summary> /// 自增count,返回自增后的值 /// </summary> public long IncrBy(string key, int count) { return base.iClient.IncrementValueBy(key, count); } /// <summary> /// 自减1,返回自减后的值 /// </summary> public long Decr(string key) { return base.iClient.DecrementValue(key); } /// <summary> /// 自减count ,返回自减后的值 /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public long DecrBy(string key, int count) { return base.iClient.DecrementValueBy(key, count); } #endregion }View Code
2)Hash
/// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 /// string的话要么是 001:序列化整个实体 /// 要么是 001_name: 001_pwd: 多个key-value /// Hash的话,一个hashid-{key:value;key:value;key:value;} /// 可以一次性查找实体,也可以单个,还可以单个修改 /// </summary> public class RedisHashService : RedisBase { #region 添加 /// <summary> /// 向hashid集合中添加key/value /// </summary> public bool SetEntryInHash(string hashid, string key, string value) { return base.iClient.SetEntryInHash(hashid, key, value); } /// <summary> /// 如果hashid集合中存在key/value则不添加返回false, /// 如果不存在在添加key/value,返回true /// </summary> public bool SetEntryInHashIfNotExists(string hashid, string key, string value) { return base.iClient.SetEntryInHashIfNotExists(hashid, key, value); } /// <summary> /// 存储对象T t到hash集合中 /// 需要包含Id,然后用Id获取 /// </summary> public void StoreAsHash<T>(T t) { base.iClient.StoreAsHash<T>(t); } #endregion #region 获取 /// <summary> /// 获取对象T中ID为id的数据。 /// </summary> public T GetFromHash<T>(object id) { return base.iClient.GetFromHash<T>(id); } /// <summary> /// 获取所有hashid数据集的key/value数据集合 /// </summary> public Dictionary<string, string> GetAllEntriesFromHash(string hashid) { return base.iClient.GetAllEntriesFromHash(hashid); } /// <summary> /// 获取hashid数据集中的数据总数 /// </summary> public long GetHashCount(string hashid) { return base.iClient.GetHashCount(hashid); } /// <summary> /// 获取hashid数据集中所有key的集合 /// </summary> public List<string> GetHashKeys(string hashid) { return base.iClient.GetHashKeys(hashid); } /// <summary> /// 获取hashid数据集中的所有value集合 /// </summary> public List<string> GetHashValues(string hashid) { return base.iClient.GetHashValues(hashid); } /// <summary> /// 获取hashid数据集中,key的value数据 /// </summary> public string GetValueFromHash(string hashid, string key) { return base.iClient.GetValueFromHash(hashid, key); } /// <summary> /// 获取hashid数据集中,多个keys的value集合 /// </summary> public List<string> GetValuesFromHash(string hashid, string[] keys) { return base.iClient.GetValuesFromHash(hashid, keys); } #endregion #region 删除 /// <summary> /// 删除hashid数据集中的key数据 /// </summary> public bool RemoveEntryFromHash(string hashid, string key) { return base.iClient.RemoveEntryFromHash(hashid, key); } #endregion #region 其它 /// <summary> /// 判断hashid数据集中是否存在key的数据 /// </summary> public bool HashContainsEntry(string hashid, string key) { return base.iClient.HashContainsEntry(hashid, key); } /// <summary> /// 给hashid数据集key的value加countby,返回相加后的数据 /// </summary> public double IncrementValueInHash(string hashid, string key, double countBy) { return base.iClient.IncrementValueInHash(hashid, key, countBy); } #endregion }View Code
3)Set
/// <summary> /// Set:用哈希表来保持字符串的唯一性,没有先后顺序,存储一些集合性的数据 /// 1.共同好友、二度好友 /// 2.利用唯一性,可以统计访问网站的所有独立 IP /// </summary> public class RedisSetService : RedisBase { #region 添加 /// <summary> /// key集合中添加value值 /// </summary> public void Add(string key, string value) { base.iClient.AddItemToSet(key, value); } /// <summary> /// key集合中添加list集合 /// </summary> public void Add(string key, List<string> list) { base.iClient.AddRangeToSet(key, list); } #endregion #region 获取 /// <summary> /// 随机获取key集合中的一个值 /// </summary> public string GetRandomItemFromSet(string key) { return base.iClient.GetRandomItemFromSet(key); } /// <summary> /// 获取key集合值的数量 /// </summary> public long GetCount(string key) { return base.iClient.GetSetCount(key); } /// <summary> /// 获取所有key集合的值 /// </summary> public HashSet<string> GetAllItemsFromSet(string key) { return base.iClient.GetAllItemsFromSet(key); } #endregion #region 删除 /// <summary> /// 随机删除key集合中的一个值 /// </summary> public string RandomRemoveItemFromSet(string key) { return base.iClient.PopItemFromSet(key); } /// <summary> /// 删除key集合中的value /// </summary> public void RemoveItemFromSet(string key, string value) { base.iClient.RemoveItemFromSet(key, value); } #endregion #region 其它 /// <summary> /// 从fromkey集合中移除值为value的值,并把value添加到tokey集合中 /// </summary> public void MoveBetweenSets(string fromkey, string tokey, string value) { base.iClient.MoveBetweenSets(fromkey, tokey, value); } /// <summary> /// 返回keys多个集合中的并集,返还hashset /// </summary> public HashSet<string> GetUnionFromSets(params string[] keys) { return base.iClient.GetUnionFromSets(keys); } /// <summary> /// 返回keys多个集合中的交集,返还hashset /// </summary> public HashSet<string> GetIntersectFromSets(params string[] keys) { return base.iClient.GetIntersectFromSets(keys); } /// <summary> /// 返回keys多个集合中的差集,返还hashset /// </summary> /// <param name="fromKey">原集合</param> /// <param name="keys">其他集合</param> /// <returns>出现在原集合,但不包含在其他集合</returns> public HashSet<string> GetDifferencesFromSet(string fromKey, params string[] keys) { return base.iClient.GetDifferencesFromSet(fromKey,keys); } /// <summary> /// keys多个集合中的并集,放入newkey集合中 /// </summary> public void StoreUnionFromSets(string newkey, string[] keys) { base.iClient.StoreUnionFromSets(newkey, keys); } /// <summary> /// 把fromkey集合中的数据与keys集合中的数据对比,fromkey集合中不存在keys集合中,则把这些不存在的数据放入newkey集合中 /// </summary> public void StoreDifferencesFromSet(string newkey, string fromkey, string[] keys) { base.iClient.StoreDifferencesFromSet(newkey, fromkey, keys); } #endregion }View Code
4)ZSet
/// <summary> /// Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列 /// 1.带有权重的元素,比如一个游戏的用户得分排行榜 /// 2.比较复杂的数据结构,一般用到的场景不算太多 /// </summary> public class RedisZSetService : RedisBase { #region 添加 /// <summary> /// 添加key/value,默认分数是从1.多*10的9次方以此递增的,自带自增效果 /// </summary> public bool Add(string key, string value) { return base.iClient.AddItemToSortedSet(key, value); } /// <summary> /// 添加key/value,并设置value的分数 /// </summary> public bool AddItemToSortedSet(string key, string value, double score) { return base.iClient.AddItemToSortedSet(key, value, score); } /// <summary> /// 为key添加values集合,values集合中每个value的分数设置为score /// </summary> public bool AddRangeToSortedSet(string key, List<string> values, double score) { return base.iClient.AddRangeToSortedSet(key, values, score); } /// <summary> /// 为key添加values集合,values集合中每个value的分数设置为score /// </summary> public bool AddRangeToSortedSet(string key, List<string> values, long score) { return base.iClient.AddRangeToSortedSet(key, values, score); } #endregion #region 获取 /// <summary> /// 获取key的所有集合 /// </summary> public List<string> GetAll(string key) { return base.iClient.GetAllItemsFromSortedSet(key); } /// <summary> /// 获取key的所有集合,倒叙输出 /// </summary> public List<string> GetAllDesc(string key) { return base.iClient.GetAllItemsFromSortedSetDesc(key); } /// <summary> /// 获取集合,带分数 /// </summary> public IDictionary<string, double> GetAllWithScoresFromSortedSet(string key) { return base.iClient.GetAllWithScoresFromSortedSet(key); } /// <summary> /// 获取key为value的下标值 /// </summary> public long GetItemIndexInSortedSet(string key, string