标签:rac iclient 其他 lis lca src *** 查找 from
RedisConfigInfo 配置类
1 using System.Configuration; 2 3 namespace MyRedisDemo.Init 4 { 5 /// <summary> 6 /// redis配置文件信息 7 /// </summary> 8 public sealed class RedisConfigInfo 9 { 10 /// <summary> 11 /// 可写的Redis链接地址 12 /// format:ip1,ip2 13 /// </summary> 14 public string WriteServerList = "127.0.0.1:6379"; 15 /// <summary> 16 /// 可读的Redis链接地址 17 /// format:ip1,ip2 18 /// </summary> 19 public string ReadServerList = "127.0.0.1:6379"; 20 /// <summary> 21 /// 最大写链接数 22 /// </summary> 23 public int MaxWritePoolSize = 60; 24 /// <summary> 25 /// 最大读链接数 26 /// </summary> 27 public int MaxReadPoolSize = 60; 28 /// <summary> 29 /// 本地缓存到期时间,单位:秒 30 /// </summary> 31 public int LocalCacheTime = 180; 32 /// <summary> 33 /// 自动重启 34 /// </summary> 35 public bool AutoStart = true; 36 /// <summary> 37 /// 是否记录日志,该设置仅用于排查redis运行时出现的问题, 38 /// 如redis工作正常,请关闭该项 39 /// </summary> 40 public bool RecordeLog = false; 41 } 42 }
RedisManager 初始化链接池管理对象
1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace MyRedisDemo.Init 9 { 10 public class RedisManager 11 { 12 /// <summary> 13 /// redis配置文件信息 14 /// </summary> 15 private static RedisConfigInfo RedisConfigInfo = new RedisConfigInfo(); 16 17 private static PooledRedisClientManager prcManager; 18 19 /// <summary> 20 /// 静态构造方法,初始化链接池管理对象 21 /// </summary> 22 static RedisManager() 23 { 24 CreateManager(); 25 } 26 27 /// <summary> 28 /// 创建链接池管理对象 29 /// </summary> 30 private static void CreateManager() 31 { 32 string[] WriteServerConStr = RedisConfigInfo.WriteServerList.Split(‘,‘); 33 string[] ReadServerConStr = RedisConfigInfo.ReadServerList.Split(‘,‘); 34 prcManager = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr, 35 new RedisClientManagerConfig 36 { 37 MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize, 38 MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize, 39 AutoStart = RedisConfigInfo.AutoStart, 40 }); 41 } 42 43 /// <summary> 44 /// 客户端缓存操作对象 45 /// </summary> 46 public static IRedisClient GetClient() 47 { 48 return prcManager.GetClient(); 49 } 50 } 51 }
RedisBase 是redis操作的基类,继承自IDisposable接口,主要用于释放内存
1 using MyRedisDemo.Init; 2 using ServiceStack.Redis; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MyRedisDemo.Interface 10 { 11 /// <summary> 12 /// RedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存 13 /// </summary> 14 public abstract class RedisBase : IDisposable 15 { 16 public static IRedisClient iClient { get; private set; } 17 private bool _disposed = false; 18 static RedisBase() 19 { 20 iClient = RedisManager.GetClient(); 21 } 22 23 public virtual void FlushAll() 24 { 25 iClient.FlushAll(); 26 } 27 protected virtual void Dispose(bool disposing) 28 { 29 if (!this._disposed) 30 { 31 if (disposing) 32 { 33 iClient.Dispose(); 34 iClient = null; 35 } 36 } 37 this._disposed = true; 38 } 39 public void Dispose() 40 { 41 Dispose(true); 42 GC.SuppressFinalize(this); 43 } 44 /// <summary> 45 /// 保存数据DB文件到硬盘 46 /// </summary> 47 public void Save() 48 { 49 iClient.Save(); 50 } 51 /// <summary> 52 /// 异步保存数据DB文件到硬盘 53 /// </summary> 54 public void SaveAsync() 55 { 56 iClient.SaveAsync(); 57 } 58 } 59 }
RedisStringService 是redis的String操作
1 using MyRedisDemo.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace MyRedisDemo.Service 9 { 10 /// <summary> 11 /// key-value 键值对:value可以是序列化的数据 12 /// </summary> 13 public class RedisStringService : RedisBase 14 { 15 #region 赋值 16 /// <summary> 17 /// 设置key的value 18 /// </summary> 19 public bool Set(string key, string value) 20 { 21 return RedisBase.iClient.Set<string>(key, value); 22 } 23 /// <summary> 24 /// 设置key的value并设置过期时间 25 /// </summary> 26 public bool Set(string key, string value, DateTime dt) 27 { 28 return RedisBase.iClient.Set<string>(key, value, dt); 29 } 30 /// <summary> 31 /// 设置key的value并设置过期时间 32 /// </summary> 33 public bool Set(string key, string value, TimeSpan sp) 34 { 35 return RedisBase.iClient.Set<string>(key, value, sp); 36 } 37 /// <summary> 38 /// 设置多个key/value 39 /// </summary> 40 public void Set(Dictionary<string, string> dic) 41 { 42 RedisBase.iClient.SetAll(dic); 43 } 44 45 #endregion 46 47 #region 追加 48 /// <summary> 49 /// 在原有key的value值之后追加value 50 /// </summary> 51 public long Append(string key, string value) 52 { 53 return RedisBase.iClient.AppendToValue(key, value); 54 } 55 #endregion 56 57 #region 获取值 58 /// <summary> 59 /// 获取key的value值 60 /// </summary> 61 public string Get(string key) 62 { 63 return RedisBase.iClient.GetValue(key); 64 } 65 /// <summary> 66 /// 获取多个key的value值 67 /// </summary> 68 public List<string> Get(List<string> keys) 69 { 70 return RedisBase.iClient.GetValues(keys); 71 } 72 /// <summary> 73 /// 获取多个key的value值 74 /// </summary> 75 public List<T> Get<T>(List<string> keys) 76 { 77 return RedisBase.iClient.GetValues<T>(keys); 78 } 79 #endregion 80 81 #region 获取旧值赋上新值 82 /// <summary> 83 /// 获取旧值赋上新值 84 /// </summary> 85 public string GetAndSetValue(string key, string value) 86 { 87 return RedisBase.iClient.GetAndSetValue(key, value); 88 } 89 #endregion 90 91 #region 辅助方法 92 /// <summary> 93 /// 获取值的长度 94 /// </summary> 95 public long GetLength(string key) 96 { 97 return RedisBase.iClient.GetStringCount(key); 98 } 99 /// <summary> 100 /// 自增1,返回自增后的值 101 /// </summary> 102 public long Incr(string key) 103 { 104 return RedisBase.iClient.IncrementValue(key); 105 } 106 /// <summary> 107 /// 自增count,返回自增后的值 108 /// </summary> 109 public double IncrBy(string key, double count) 110 { 111 return RedisBase.iClient.IncrementValueBy(key, count); 112 } 113 /// <summary> 114 /// 自减1,返回自减后的值 115 /// </summary> 116 public long Decr(string key) 117 { 118 return RedisBase.iClient.DecrementValue(key); 119 } 120 /// <summary> 121 /// 自减count ,返回自减后的值 122 /// </summary> 123 /// <param name="key"></param> 124 /// <param name="count"></param> 125 /// <returns></returns> 126 public long DecrBy(string key, int count) 127 { 128 return RedisBase.iClient.DecrementValueBy(key, count); 129 } 130 #endregion 131 } 132 }
RedisListService 是redis的List操作
1 using MyRedisDemo.Interface; 2 using ServiceStack.Redis; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MyRedisDemo.Service 10 { 11 /// <summary> 12 /// Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销, 13 /// Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构。 14 /// </summary> 15 public class RedisListService : RedisBase 16 { 17 #region 赋值 18 /// <summary> 19 /// 从左侧向list中添加值 20 /// </summary> 21 public void LPush(string key, string value) 22 { 23 RedisBase.iClient.PushItemToList(key, value); 24 } 25 /// <summary> 26 /// 从左侧向list中添加值,并设置过期时间 27 /// </summary> 28 public void LPush(string key, string value, DateTime dt) 29 { 30 RedisBase.iClient.PushItemToList(key, value); 31 RedisBase.iClient.ExpireEntryAt(key, dt); 32 } 33 /// <summary> 34 /// 从左侧向list中添加值,设置过期时间 35 /// </summary> 36 public void LPush(string key, string value, TimeSpan sp) 37 { 38 RedisBase.iClient.PushItemToList(key, value); 39 RedisBase.iClient.ExpireEntryIn(key, sp); 40 } 41 /// <summary> 42 /// 从左侧向list中添加值 43 /// </summary> 44 public void RPush(string key, string value) 45 { 46 RedisBase.iClient.PrependItemToList(key, value); 47 } 48 /// <summary> 49 /// 从右侧向list中添加值,并设置过期时间 50 /// </summary> 51 public void RPush(string key, string value, DateTime dt) 52 { 53 RedisBase.iClient.PrependItemToList(key, value); 54 RedisBase.iClient.ExpireEntryAt(key, dt); 55 } 56 /// <summary> 57 /// 从右侧向list中添加值,并设置过期时间 58 /// </summary> 59 public void RPush(string key, string value, TimeSpan sp) 60 { 61 RedisBase.iClient.PrependItemToList(key, value); 62 RedisBase.iClient.ExpireEntryIn(key, sp); 63 } 64 /// <summary> 65 /// 添加key/value 66 /// </summary> 67 public void Add(string key, string value) 68 { 69 RedisBase.iClient.AddItemToList(key, value); 70 } 71 /// <summary> 72 /// 添加key/value ,并设置过期时间 73 /// </summary> 74 public void Add(string key, string value, DateTime dt) 75 { 76 RedisBase.iClient.AddItemToList(key, value); 77 RedisBase.iClient.ExpireEntryAt(key, dt); 78 } 79 /// <summary> 80 /// 添加key/value。并添加过期时间 81 /// </summary> 82 public void Add(string key, string value, TimeSpan sp) 83 { 84 RedisBase.iClient.AddItemToList(key, value); 85 RedisBase.iClient.ExpireEntryIn(key, sp); 86 } 87 /// <summary> 88 /// 为key添加多个值 89 /// </summary> 90 public void Add(string key, List<string> values) 91 { 92 RedisBase.iClient.AddRangeToList(key, values); 93 } 94 /// <summary> 95 /// 为key添加多个值,并设置过期时间 96 /// </summary> 97 public void Add(string key, List<string> values, DateTime dt) 98 { 99 RedisBase.iClient.AddRangeToList(key, values); 100 RedisBase.iClient.ExpireEntryAt(key, dt); 101 } 102 /// <summary> 103 /// 为key添加多个值,并设置过期时间 104 /// </summary> 105 public void Add(string key, List<string> values, TimeSpan sp) 106 { 107 RedisBase.iClient.AddRangeToList(key, values); 108 RedisBase.iClient.ExpireEntryIn(key, sp); 109 } 110 #endregion 111 112 #region 获取值 113 /// <summary> 114 /// 获取list中key包含的数据数量 115 /// </summary> 116 public long Count(string key) 117 { 118 return RedisBase.iClient.GetListCount(key); 119 } 120 /// <summary> 121 /// 获取key包含的所有数据集合 122 /// </summary> 123 public List<string> Get(string key) 124 { 125 return RedisBase.iClient.GetAllItemsFromList(key); 126 } 127 /// <summary> 128 /// 获取key中下标为star到end的值集合 129 /// </summary> 130 public List<string> Get(string key, int star, int end) 131 { 132 return RedisBase.iClient.GetRangeFromList(key, star, end); 133 } 134 #endregion 135 136 #region 阻塞命令 137 /// <summary> 138 /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp 139 /// </summary> 140 public string BlockingPopItemFromList(string key, TimeSpan? sp) 141 { 142 return RedisBase.iClient.BlockingDequeueItemFromList(key, sp); 143 } 144 /// <summary> 145 /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp 146 /// </summary> 147 public ItemRef BlockingPopItemFromLists(string[] keys, TimeSpan? sp) 148 { 149 return RedisBase.iClient.BlockingPopItemFromLists(keys, sp); 150 } 151 /// <summary> 152 /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp 153 /// </summary> 154 public string BlockingDequeueItemFromList(string key, TimeSpan? sp) 155 { 156 return RedisBase.iClient.BlockingDequeueItemFromList(key, sp); 157 } 158 /// <summary> 159 /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp 160 /// </summary> 161 public ItemRef BlockingDequeueItemFromLists(string[] keys, TimeSpan? sp) 162 { 163 return RedisBase.iClient.BlockingDequeueItemFromLists(keys, sp); 164 } 165 /// <summary> 166 /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp 167 /// </summary> 168 public string BlockingRemoveStartFromList(string keys, TimeSpan? sp) 169 { 170 return RedisBase.iClient.BlockingRemoveStartFromList(keys, sp); 171 } 172 /// <summary> 173 /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp 174 /// </summary> 175 public ItemRef BlockingRemoveStartFromLists(string[] keys, TimeSpan? sp) 176 { 177 return RedisBase.iClient.BlockingRemoveStartFromLists(keys, sp); 178 } 179 /// <summary> 180 /// 阻塞命令:从list中一个fromkey的尾部移除一个值,添加到另外一个tokey的头部,并返回移除的值,阻塞时间为sp 181 /// </summary> 182 public string BlockingPopAndPushItemBetweenLists(string fromkey, string tokey, TimeSpan? sp) 183 { 184 return RedisBase.iClient.BlockingPopAndPushItemBetweenLists(fromkey, tokey, sp); 185 } 186 #endregion 187 188 #region 删除 189 /// <summary> 190 /// 从尾部移除数据,返回移除的数据 191 /// </summary> 192 public string PopItemFromList(string key) 193 { 194 return RedisBase.iClient.PopItemFromList(key); 195 } 196 /// <summary> 197 /// 移除list中,key/value,与参数相同的值,并返回移除的数量 198 /// </summary> 199 public long RemoveItemFromList(string key, string value) 200 { 201 return RedisBase.iClient.RemoveItemFromList(key, value); 202 } 203 /// <summary> 204 /// 从list的尾部移除一个数据,返回移除的数据 205 /// </summary> 206 public string RemoveEndFromList(string key) 207 { 208 return RedisBase.iClient.RemoveEndFromList(key); 209 } 210 /// <summary> 211 /// 从list的头部移除一个数据,返回移除的值 212 /// </summary> 213 public string RemoveStartFromList(string key) 214 { 215 return RedisBase.iClient.RemoveStartFromList(key); 216 } 217 #endregion 218 219 #region 其它 220 /// <summary> 221 /// 从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值 222 /// </summary> 223 public string PopAndPushItemBetweenLists(string fromKey, string toKey) 224 { 225 return RedisBase.iClient.PopAndPushItemBetweenLists(fromKey, toKey); 226 } 227 #endregion 228 } 229 }
RedisSetService 是redis的Set操作
1 using MyRedisDemo.Interface; 2 using ServiceStack.Redis; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MyRedisDemo.Service 10 { 11 /// <summary> 12 /// Set:用哈希表来保持字符串的唯一性,没有先后顺序,存储一些集合性的数据 13 /// 1.共同好友、二度好友 14 /// 2.利用唯一性,可以统计访问网站的所有独立 IP 15 /// </summary> 16 public class RedisSetService : RedisBase 17 { 18 #region 添加 19 /// <summary> 20 /// key集合中添加value值 21 /// </summary> 22 public void Add(string key, string value) 23 { 24 RedisBase.iClient.AddItemToSet(key, value); 25 } 26 /// <summary> 27 /// key集合中添加list集合 28 /// </summary> 29 public void Add(string key, List<string> list) 30 { 31 RedisBase.iClient.AddRangeToSet(key, list); 32 } 33 #endregion 34 35 #region 获取 36 /// <summary> 37 /// 随机获取key集合中的一个值 38 /// </summary> 39 public string GetRandomItemFromSet(string key) 40 { 41 return RedisBase.iClient.GetRandomItemFromSet(key); 42 } 43 /// <summary> 44 /// 获取key集合值的数量 45 /// </summary> 46 public long GetCount(string key) 47 { 48 return RedisBase.iClient.GetSetCount(key); 49 } 50 /// <summary> 51 /// 获取所有key集合的值 52 /// </summary> 53 public HashSet<string> GetAllItemsFromSet(string key) 54 { 55 return RedisBase.iClient.GetAllItemsFromSet(key); 56 } 57 #endregion 58 59 #region 删除 60 /// <summary> 61 /// 随机删除key集合中的一个值 62 /// </summary> 63 public string RandomRemoveItemFromSet(string key) 64 { 65 return RedisBase.iClient.PopItemFromSet(key); 66 } 67 /// <summary> 68 /// 删除key集合中的value 69 /// </summary> 70 public void RemoveItemFromSet(string key, string value) 71 { 72 RedisBase.iClient.RemoveItemFromSet(key, value); 73 } 74 #endregion 75 76 #region 其它 77 /// <summary> 78 /// 从fromkey集合中移除值为value的值,并把value添加到tokey集合中 79 /// </summary> 80 public void MoveBetweenSets(string fromkey, string tokey, string value) 81 { 82 RedisBase.iClient.MoveBetweenSets(fromkey, tokey, value); 83 } 84 /// <summary> 85 /// 返回keys多个集合中的并集,返还hashset 86 /// </summary> 87 public HashSet<string> GetUnionFromSets(params string[] keys) 88 { 89 return RedisBase.iClient.GetUnionFromSets(keys); 90 } 91 /// <summary> 92 /// 返回keys多个集合中的交集,返还hashset 93 /// </summary> 94 public HashSet<string> GetIntersectFromSets(params string[] keys) 95 { 96 return RedisBase.iClient.GetIntersectFromSets(keys); 97 } 98 /// <summary> 99 /// 返回keys多个集合中的差集,返还hashset 100 /// </summary> 101 /// <param name="fromKey">原集合</param> 102 /// <param name="keys">其他集合</param> 103 /// <returns>出现在原集合,但不包含在其他集合</returns> 104 public HashSet<string> GetDifferencesFromSet(string fromKey, params string[] keys) 105 { 106 return RedisBase.iClient.GetDifferencesFromSet(fromKey,keys); 107 } 108 /// <summary> 109 /// keys多个集合中的并集,放入newkey集合中 110 /// </summary> 111 public void StoreUnionFromSets(string newkey, string[] keys) 112 { 113 RedisBase.iClient.StoreUnionFromSets(newkey, keys); 114 } 115 /// <summary> 116 /// 把fromkey集合中的数据与keys集合中的数据对比,fromkey集合中不存在keys集合中,则把这些不存在的数据放入newkey集合中 117 /// </summary> 118 public void StoreDifferencesFromSet(string newkey, string fromkey, string[] keys) 119 { 120 RedisBase.iClient.StoreDifferencesFromSet(newkey, fromkey, keys); 121 } 122 #endregion 123 } 124 }
RedisZSetService 是redis的ZSet操作
1 using MyRedisDemo.Interface; 2 using ServiceStack.Redis; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MyRedisDemo.Service 10 { 11 /// <summary> 12 /// Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列 13 /// 1.带有权重的元素,比如一个游戏的用户得分排行榜 14 /// 2.比较复杂的数据结构,一般用到的场景不算太多 15 /// </summary> 16 public class RedisZSetService : RedisBase 17 { 18 #region 添加 19 /// <summary> 20 /// 添加key/value,默认分数是从1.多*10的9次方以此递增的,自带自增效果 21 /// </summary> 22 public bool Add(string key, string value) 23 { 24 return RedisBase.iClient.AddItemToSortedSet(key, value); 25 } 26 /// <summary> 27 /// 添加key/value,并设置value的分数 28 /// </summary> 29 public bool AddItemToSortedSet(string key, string value, double score) 30 { 31 return RedisBase.iClient.AddItemToSortedSet(key, value, score); 32 } 33 /// <summary> 34 /// 为key添加values集合,values集合中每个value的分数设置为score 35 /// </summary> 36 public bool AddRangeToSortedSet(string key, List<string> values, double score) 37 { 38 return RedisBase.iClient.AddRangeToSortedSet(key, values, score); 39 } 40 /// <summary> 41 /// 为key添加values集合,values集合中每个value的分数设置为score 42 /// </summary> 43 public bool AddRangeToSortedSet(string key, List<string> values, long score) 44 { 45 return RedisBase.iClient.AddRangeToSortedSet(key, values, score); 46 } 47 #endregion 48 49 #region 获取 50 /// <summary> 51 /// 获取key的所有集合 52 /// </summary> 53 public List<string> GetAll(string key) 54 { 55 return RedisBase.iClient.GetAllItemsFromSortedSet(key); 56 } 57 /// <summary> 58 /// 获取key的所有集合,倒叙输出 59 /// </summary> 60 public List<string> GetAllDesc(string key) 61 { 62 return RedisBase.iClient.GetAllItemsFromSortedSetDesc(key); 63 } 64 /// <summary> 65 /// 获取集合,带分数 66 /// </summary> 67 public IDictionary<string, double> GetAllWithScoresFromSortedSet(string key) 68 { 69 return RedisBase.iClient.GetAllWithScoresFromSortedSet(key); 70 } 71 /// <summary> 72 /// 获取key为value的下标值 73 /// </summary> 74 public long GetItemIndexInSortedSet(string key, string value) 75 { 76 return RedisBase.iClient.GetItemIndexInSortedSet(key, value); 77 } 78 /// <summary> 79 /// 倒叙排列获取key为value的下标值 80 /// </summary> 81 public long GetItemIndexInSortedSetDesc(string key, string value) 82 { 83 return RedisBase.iClient.GetItemIndexInSortedSetDesc(key, value); 84 } 85 /// <summary> 86 /// 获取key为value的分数 87 /// </summary> 88 public double GetItemScoreInSortedSet(string key, string value) 89 { 90 return RedisBase.iClient.GetItemScoreInSortedSet(key, value); 91 } 92 /// <summary> 93 /// 获取key所有集合的数据总数 94 /// </summary> 95 public long GetSortedSetCount(string key) 96 { 97 return RedisBase.iClient.GetSortedSetCount(key); 98 } 99 /// <summary> 100 /// key集合数据从分数为fromscore到分数为toscore的数据总数 101 /// </summary> 102 public long GetSortedSetCount(string key, double fromScore, double toScore) 103 { 104 return RedisBase.iClient.GetSortedSetCount(key, fromScore, toScore); 105 } 106 /// <summary> 107 /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据 108 /// </summary> 109 public List<string> GetRangeFromSortedSetByHighestScore(string key, double fromscore, double toscore) 110 { 111 return RedisBase.iClient.GetRangeFromSortedSetByHighestScore(key, fromscore, toscore); 112 } 113 /// <summary> 114 /// 获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据 115 /// </summary> 116 public List<string> GetRangeFromSortedSetByLowestScore(string key, double fromscore, double toscore) 117 { 118 return RedisBase.iClient.GetRangeFromSortedSetByLowestScore(key, fromscore, toscore); 119 } 120 /// <summary> 121 /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据,带分数 122 /// </summary> 123 public IDictionary<string, double> GetRangeWithScoresFromSortedSetByHighestScore(string key, double fromscore, double toscore) 124 { 125 return RedisBase.iClient.GetRangeWithScoresFromSortedSetByHighestScore(key, fromscore, toscore); 126 } 127 /// <summary> 128 /// 获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据,带分数 129 /// </summary> 130 public IDictionary<string, double> GetRangeWithScoresFromSortedSetByLowestScore(string key, double fromscore, double toscore) 131 { 132 return RedisBase.iClient.GetRangeWithScoresFromSortedSetByLowestScore(key, fromscore, toscore); 133 } 134 /// <summary> 135 /// 获取key集合数据,下标从fromRank到分数为toRank的数据 136 /// </summary> 137 public List<string> GetRangeFromSortedSet(string key, int fromRank, int toRank) 138 { 139 return RedisBase.iClient.GetRangeFromSortedSet(key, fromRank, toRank); 140 } 141 /// <summary> 142 /// 获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据 143 /// </summary> 144 public List<string> GetRangeFromSortedSetDesc(string key, int fromRank, int toRank) 145 { 146 return RedisBase.iClient.GetRangeFromSortedSetDesc(key, fromRank, toRank); 147 } 148 /// <summary> 149 /// 获取key集合数据,下标从fromRank到分数为toRank的数据,带分数 150 /// </summary> 151 public IDictionary<string, double> GetRangeWithScoresFromSortedSet(string key, int fromRank, int toRank) 152 { 153 return RedisBase.iClient.GetRangeWithScoresFromSortedSet(key, fromRank, toRank); 154 } 155 /// <summary> 156 /// 获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据,带分数 157 /// </summary> 158 public IDictionary<string, double> GetRangeWithScoresFromSortedSetDesc(string key, int fromRank, int toRank) 159 { 160 return RedisBase.iClient.GetRangeWithScoresFromSortedSetDesc(key, fromRank, toRank); 161 } 162 #endregion 163 164 #region 删除 165 /// <summary> 166 /// 删除key为value的数据 167 /// </summary> 168 public bool RemoveItemFromSortedSet(string key, string value) 169 { 170 return RedisBase.iClient.RemoveItemFromSortedSet(key, value); 171 } 172 /// <summary> 173 /// 删除下标从minRank到maxRank的key集合数据 174 /// </summary> 175 public long RemoveRangeFromSortedSet(string key, int minRank, int maxRank) 176 { 177 return RedisBase.iClient.RemoveRangeFromSortedSet(key, minRank, maxRank); 178 } 179 /// <summary> 180 /// 删除分数从fromscore到toscore的key集合数据 181 /// </summary> 182 public long RemoveRangeFromSortedSetByScore(string key, double fromscore, double toscore) 183 { 184 return RedisBase.iClient.RemoveRangeFromSortedSetByScore(key, fromscore, toscore); 185 } 186 /// <summary> 187 /// 删除key集合中分数最大的数据 188 /// </summary> 189 public string PopItemWithHighestScoreFromSortedSet(string key) 190 { 191 return RedisBase.iClient.PopItemWithHighestScoreFromSortedSet(key); 192 } 193 /// <summary> 194 /// 删除key集合中分数最小的数据 195 /// </summary> 196 public string PopItemWithLowestScoreFromSortedSet(string key) 197 { 198 return RedisBase.iClient.PopItemWithLowestScoreFromSortedSet(key); 199 } 200 #endregion 201 202 #region 其它 203 /// <summary> 204 /// 判断key集合中是否存在value数据 205 /// </summary> 206 public bool SortedSetContainsItem(string key, string value) 207 { 208 return RedisBase.iClient.SortedSetContainsItem(key, value); 209 } 210 /// <summary> 211 /// 为key集合值为value的数据,分数加scoreby,返回相加后的分数 212 /// </summary> 213 public double IncrementItemInSortedSet(string key, string value, double scoreBy) 214 { 215 return RedisBase.iClient.IncrementItemInSortedSet(key, value, scoreBy); 216 } 217 /// <summary> 218 /// 获取keys多个集合的交集,并把交集添加的newkey集合中,返回交集数据的总数 219 /// </summary> 220 public long StoreIntersectFromSortedSets(string newkey, string[] keys) 221 { 222 return RedisBase.iClient.StoreIntersectFromSortedSets(newkey, keys); 223 } 224 /// <summary> 225 /// 获取keys多个集合的并集,并把并集数据添加到newkey集合中,返回并集数据的总数 226 /// </summary> 227 public long StoreUnionFromSortedSets(string newkey, string[] keys) 228 { 229 return RedisBase.iClient.StoreUnionFromSortedSets(newkey, keys); 230 } 231 #endregion 232 } 233 }
RedisHashService 是redis的Hash操作
1 using MyRedisDemo.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace MyRedisDemo.Service 9 { 10 /// <summary> 11 /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 12 /// string的话要么是 001:序列化整个实体 13 /// 要么是 001_name: 001_pwd: 多个key-value 14 /// Hash的话,一个hashid-{key:value;key:value;key:value;} 15 /// 可以一次性查找实体,也可以单个,还可以单个修改 16 /// </summary> 17 public class RedisHashService : RedisBase 18 { 19 #region 添加 20 /// <summary> 21 /// 向hashid集合中添加key/value 22 /// </summary> 23 public bool SetEntryInHash(string hashid, string key, string value) 24 { 25 return RedisBase.iClient.SetEntryInHash(hashid, key, value); 26 } 27 /// <summary> 28 /// 如果hashid集合中存在key/value则不添加返回false, 29 /// 如果不存在在添加key/value,返回true 30 /// </summary> 31 public bool SetEntryInHashIfNotExists(string hashid, string key, string value) 32 { 33 return RedisBase.iClient.SetEntryInHashIfNotExists(hashid, key, value); 34 } 35 /// <summary> 36 /// 存储对象T t到hash集合中 37 /// </summary> 38 public void StoreAsHash<T>(T t) 39 { 40 RedisBase.iClient.StoreAsHash<T>(t); 41 } 42 #endregion 43 44 #region 获取 45 /// <summary> 46 /// 获取对象T中ID为id的数据。 47 /// </summary> 48 public T GetFromHash<T>(object id) 49 { 50 return RedisBase.iClient.GetFromHash<T>(id); 51 } 52 /// <summary> 53 /// 获取所有hashid数据集的key/value数据集合 54 /// </summary> 55 public Dictionary<string, string> GetAllEntriesFromHash(string hashid) 56 { 57 return RedisBase.iClient.GetAllEntriesFromHash(hashid); 58 } 59 /// <summary> 60 /// 获取hashid数据集中的数据总数 61 /// </summary> 62 public long GetHashCount(string hashid) 63 { 64 return RedisBase.iClient.GetHashCount(hashid); 65 } 66 /// <summary> 67 /// 获取hashid数据集中所有key的集合 68 /// </summary> 69 public List<string> GetHashKeys(string hashid) 70 { 71 return RedisBase.iClient.GetHashKeys(hashid); 72 } 73 /// <summary> 74 /// 获取hashid数据集中的所有value集合 75 /// </summary> 76 public List<string> GetHashValues(string hashid) 77 { 78 return RedisBase.iClient.GetHashValues(hashid); 79 } 80 /// <summary> 81 /// 获取hashid数据集中,key的value数据 82 /// </summary> 83 public string GetValueFromHash(string hashid, string key) 84 { 85 return RedisBase.iClient.GetValueFromHash(hashid, key); 86 } 87 /// <summary> 88 /// 获取hashid数据集中,多个keys的value集合 89 /// </summary> 90 public List<string> GetValuesFromHash(string hashid, string[] keys) 91 { 92 return RedisBase.iClient.GetValuesFromHash(hashid, keys); 93 } 94 #endregion 95 96 #region 删除 97 /// <summary> 98 /// 删除hashid数据集中的key数据 99 /// </summary> 100 public bool RemoveEntryFromHash(string hashid, string key) 101 { 102 return RedisBase.iClient.RemoveEntryFromHash(hashid, key); 103 } 104 #endregion 105 106 #region 其它 107 /// <summary> 108 /// 判断hashid数据集中是否存在key的数据 109 /// </summary> 110 public bool HashContainsEntry(string hashid, string key) 111 { 112 return RedisBase.iClient.HashContainsEntry(hashid, key); 113 } 114 /// <summary> 115 /// 给hashid数据集key的value加countby,返回相加后的数据 116 /// </summary> 117 public double IncrementValueInHash(string hashid, string key, double countBy) 118 { 119 return RedisBase.iClient.IncrementValueInHash(hashid, key, countBy); 120 } 121 #endregion 122 } 123 }
Student 实体类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace MyRedisDemo 8 { 9 public class Student 10 { 11 public int Id { get; set; } 12 public string Name { get; set; } 13 public string Remark { get; set; } 14 public string Description { get; set; } 15 } 16 }
调用
1 using MyRedisDemo.Service; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading; 7 using System.Threading.Tasks; 8 9 namespace MyRedisDemo 10 { 11 ///<summary> 12 /// 1 Cache和NoSql 13 /// 2 String 14 /// 3 Hashtable 15 /// 4 Set 16 /// 5 ZSet 17 /// 6 List 18 /// 7 分布式异步队列 19 20 ///</summary> 21 class Program 22 { 23 static void Main(string[] args) 24 { 25 try 26 { 27 Student student_1 = new Student() 28 { 29 Id = 11, 30 Name = "Eleven" 31 }; 32 Student student_2 = new Student() 33 { 34 Id = 12, 35 Name = "Twelve", 36 Remark = "123423245" 37 }; 38 39 //Student_2_id 12 Student_2_Name Twelve 40 //需要修改对象 41 //查询--反序列化--修改--序列化保存 42 Console.WriteLine("*****************************************"); 43 { 44 //RedisStringService service = new RedisStringService(); 45 //service.FlushAll(); 46 //service.Set("RedisStringService_key1", "RedisStringService_value1"); 47 //Console.WriteLine(service.Get("RedisStringService_key1")); 48 //Console.WriteLine(service.GetAndSetValue("RedisStringService_key1", "RedisStringService_value2")); 49 //Console.WriteLine(service.Get("RedisStringService_key1")); 50 51 //service.Append("RedisStringService_key1", "Append"); 52 //Console.WriteLine(service.Get("RedisStringService_key1")); 53 //service.Set("RedisStringService_key1", "RedisStringService_value", DateTime.Now.AddSeconds(5)); 54 //Console.WriteLine(service.Get("RedisStringService_key1")); 55 //Thread.Sleep(5000); 56 //Console.WriteLine(service.Get("RedisStringService_key1")); 57 58 } 59 60 Console.WriteLine("*****************************************"); 61 { 62 //RedisHashService service = new RedisHashService(); 63 //service.FlushAll(); 64 //service.SetEntryInHash("Student", "id", "13"); 65 //service.SetEntryInHash("Student", "Name", "Thirteen"); 66 //service.SetEntryInHashIfNotExists("Student", "Remark", "1234567"); 67 //var listResult = service.GetHashValues("Student"); 68 //listResult = service.GetHashKeys("Student"); 69 70 //var dicList = service.GetAllEntriesFromHash("Student"); 71 72 //service.SetEntryInHash("Student", "id", "14");//同一条数据,覆盖 73 //service.SetEntryInHash("Student", "Name", "Fourteen"); 74 //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678");//同一条数据,不覆盖 75 76 //listResult = service.GetHashValues("Student"); 77 //service.RemoveEntryFromHash("Student", "Remark"); 78 //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678"); 79 //listResult = service.GetHashValues("Student"); 80 81 //service.StoreAsHash<Student>(student_1); 82 //Student student1 = service.GetFromHash<Student>(11); 83 //service.StoreAsHash<Student>(student_2); 84 //Student student2 = service.GetFromHash<Student>(12); 85 } 86 Console.WriteLine("*****************************************"); 87 { 88 ////key--values 89 //RedisSetService service = new RedisSetService(); 90 //service.FlushAll(); 91 //service.Add("Advanced", "111"); 92 //service.Add("Advanced", "112"); 93 //service.Add("Advanced", "113"); 94 //service.Add("Advanced", "114"); 95 //service.Add("Advanced", "115"); 96 //service.Add("Advanced", "111"); 97 98 //service.Add("Begin", "111"); 99 //service.Add("Begin", "112"); 100 //service.Add("Begin", "113"); 101 //service.Add("Begin", "116"); 102 //service.Add("Begin", "117"); 103 //service.Add("Begin", "111"); 104 105 //service.Add("Internal", "111"); 106 //service.Add("Internal", "112"); 107 //service.Add("Internal", "117"); 108 //service.Add("Internal", "118"); 109 //service.Add("Internal", "119"); 110 //service.Add("Internal", "111"); 111 112 //var result = service.GetAllItemsFromSet("Advanced"); 113 //var result2 = service.GetRandomItemFromSet("Advanced"); 114 //result = service.GetAllItemsFromSet("Begin"); 115 //result2 = service.GetRandomItemFromSet("Begin"); 116 117 //var result3 = service.GetIntersectFromSets("Advanced", "Begin", "Internal");//交 118 //result3 = service.GetDifferencesFromSet("Advanced", "Begin", "Internal");//差 119 //result3 = service.GetUnionFromSets("Advanced", "Begin", "Internal");//并 120 121 //service.RemoveItemFromSet("Advanced", "111"); 122 //result = service.GetAllItemsFromSet("Advanced"); 123 //service.RandomRemoveItemFromSet("Advanced"); 124 //result = service.GetAllItemsFromSet("Advanced"); 125 } 126 Console.WriteLine("*****************************************"); 127 { 128 //RedisZSetService service = new RedisZSetService(); 129 //service.FlushAll(); 130 //service.Add("score", "111"); 131 //service.Add("score", "112"); 132 //service.Add("score", "113"); 133 //service.Add("score", "114"); 134 //service.Add("score", "115"); 135 //service.Add("score", "111"); 136 137 //service.AddItemToSortedSet("user", "Eleven1", 1); 138 139 //service.AddItemToSortedSet("user", "Eleven2", 2); 140 141 //var list = service.GetAll("score"); 142 //list = service.GetAllDesc("score"); 143 } 144 Console.WriteLine("*****************************************"); 145 { 146 RedisListService service = new RedisListService(); 147 service.FlushAll(); 148 149 List<string> stringList = new List<string>(); 150 for (int i = 0; i < 10; i++) 151 { 152 stringList.Add(string.Format("放入任务{0}", i)); 153 } 154 155 service.LPush("test", "这是一个学生1"); 156 service.LPush("test", "这是一个学生2"); 157 service.LPush("test", "这是一个学生3"); 158 service.LPush("test", "这是一个学生4"); 159 service.Add("task", stringList); 160 161 //Console.WriteLine(service.Count("test")); 162 //Console.WriteLine(service.Count("task")); 163 //var list = service.Get("test"); 164 //list = service.Get("task", 2, 4); 165 166 167 //new Action(() => 168 //{ 169 // while (true) 170 // { 171 // var result = service.BlockingPopItemFromLists(new string[] { "test", "task" }, TimeSpan.FromHours(3)); 172 // Console.WriteLine("这里是队列获取的消息 {0} {1}", result.Id, result.Item); 173 // } 174 //}).BeginInvoke(null, null); 175 176 Action act = new Action(() => 177 { 178 while (true) 179 { 180 Console.WriteLine("************请输入数据**************"); 181 string testTask = Console.ReadLine(); 182 service.LPush("test", testTask); 183 } 184 }); 185 act.EndInvoke(act.BeginInvoke(null, null)); 186 } 187 } 188 catch (Exception ex) 189 { 190 Console.WriteLine(ex.Message); 191 } 192 Console.Read(); 193 } 194 } 195 }
注:来至于腾讯课堂培训实例
标签:rac iclient 其他 lis lca src *** 查找 from
原文地址:https://www.cnblogs.com/anyihen/p/12831129.html