C#利用ServiceStack.Redis访问Redis
概述
Redis【REmote DIctionary Server】作为当前比较流行的NoSql数据库,以其高性能,高可用的的特点,应用越来越广泛,深得各大企业和开发人员的青睐。 本文主要以一个简单的小例子,简述ServiceStack.Redis动态库在Redis方面的相关应用,仅供学习分享使用,如有不足之处,还请指正。
开发环境
相关开发环境,如下所示:
- Microsoft Visual Studio Community 2019
- ServiceStack.Redis 动态库
- Redis 6.0.9 服务器端环境搭建
ServiceStack.Redis的安装
在C#开发中,主要通过NuGet包管理器,来安装ServiceStack.Redis动态库,目前版本为5.10.4,如下所示:
示例截图
关于C#调用Redis相关示例截图,如下所示:
核心代码
关于Redis操作的核心代码,主要分两部分:Redis客户端管理类,Redis客户端操作类。
Redis客户端管理类,主要通过Redis客户端连接池【PooledRedisClientManager】创建客户端对象【IRedisClient】,如下所示:
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 DemoRedis 9 { 10 /// <summary> 11 /// Redis客户端管理类 12 /// </summary> 13 public static class RedisManager 14 { 15 private static PooledRedisClientManager clientManager; 16 17 /// <summary> 18 /// 初始化信息 19 /// </summary> 20 private static void initInfo() 21 { 22 string ipaddr = System.Configuration.ConfigurationManager.AppSettings["ipaddr"]; 23 string port = System.Configuration.ConfigurationManager.AppSettings["port"]; 24 string host = string.Format("{0}:{1}", ipaddr, port); 25 initInfo(new string[] { host }, new string[] { host }); 26 } 27 28 /// <summary> 29 /// 初始化Redis客户端管理 30 /// </summary> 31 /// <param name="readWriteHosts"></param> 32 /// <param name="readOnlyHosts"></param> 33 private static void initInfo(string[] readWriteHosts, string[] readOnlyHosts) 34 { 35 clientManager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 36 { 37 MaxWritePoolSize = 5, // “写”链接池链接数 38 MaxReadPoolSize = 5, // “读”链接池链接数 39 AutoStart = true, 40 }); 41 } 42 43 public static IRedisClient getRedisClient() 44 { 45 if (clientManager == null) 46 { 47 initInfo(); 48 } 49 return clientManager.GetClient(); 50 } 51 52 } 53 54 55 }
Redis客户端操作类,主要包括字符串(String),列表(List),哈希结构(Hash),集合(Set),有序集合(Sorted Set)等相关操作功能的封装,如下所示:
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 DemoRedis 9 { 10 public class RedisHelper:IDisposable 11 { 12 private IRedisClient client; 13 14 public RedisHelper() 15 { 16 17 } 18 19 public RedisHelper(IRedisClient client ) { 20 this.client = client; 21 } 22 23 #region 字符串 24 25 public List<string> getAllKeys() 26 { 27 return this.client.GetAllKeys(); 28 } 29 30 /// <summary> 31 /// 设置值 32 /// </summary> 33 /// <param name="key"></param> 34 /// <param name="value"></param> 35 public void setString(string key,string value) 36 { 37 this.client.Set(key, value); 38 } 39 40 /// <summary> 41 /// 设置值,带过期时间 42 /// </summary> 43 /// <param name="key"></param> 44 /// <param name="value"></param> 45 /// <param name="expiresIn"></param> 46 public void setString(string key, string value,TimeSpan expiresIn) 47 { 48 this.client.Set(key, value, expiresIn); 49 } 50 51 /// <summary> 52 /// 是否存在 53 /// </summary> 54 /// <param name="key"></param> 55 /// <returns></returns> 56 public bool containsKey(string key) 57 { 58 return this.client.ContainsKey(key); 59 } 60 61 /// <summary> 62 /// 设置字典 63 /// </summary> 64 /// <param name="dic"></param> 65 public void setAll(Dictionary<string,string> dic) 66 { 67 this.client.SetAll(dic); 68 } 69 70 /// <summary> 71 /// 返回旧值,设置新值 72 /// </summary> 73 /// <param name="key"></param> 74 /// <param name="value"></param> 75 /// <returns></returns> 76 public string getAndSetValue(string key,string value) { 77 return this.client.GetAndSetValue(key, value); 78 } 79 80 /// <summary> 81 /// 获取对应的值 82 /// </summary> 83 /// <param name="key"></param> 84 /// <returns></returns> 85 public string getValue(string key) { 86 return this.client.GetValue(key); 87 } 88 89 public string getRandomKey() 90 { 91 return this.client.GetRandomKey(); 92 } 93 94 /// <summary> 95 /// 追加值到现有key中 96 /// </summary> 97 /// <param name="key"></param> 98 /// <param name="value"></param> 99 public void appendToValue(string key, string value) { 100 this.client.AppendToValue(key, value); 101 } 102 103 #endregion 104 105 #region List 106 107 /// <summary> 108 /// (队列操作)增加一个值到列表 109 /// </summary> 110 /// <param name="listId"></param> 111 /// <param name="value"></param> 112 public void enqueueItemOnList(string listId ,string value) 113 { 114 this.client.EnqueueItemOnList(listId, value); 115 } 116 117 /// <summary> 118 /// 从队列中出一个 119 /// </summary> 120 /// <param name="listId"></param> 121 /// <returns></returns> 122 public string dequeueItemFromList(string listId) 123 { 124 return this.client.DequeueItemFromList(listId); 125 } 126 127 /// <summary> 128 /// 栈,增加一个值到列表 129 /// </summary> 130 /// <param name="listId"></param> 131 /// <param name="value"></param> 132 public void pushItemToList(string listId, string value) { 133 this.client.PushItemToList(listId, value); 134 } 135 136 /// <summary> 137 /// 栈,从当前列表中出一个值,并返回 138 /// </summary> 139 /// <param name="listId"></param> 140 /// <returns></returns> 141 public string popItemFromList(string listId) 142 { 143 return this.client.PopItemFromList(listId); 144 } 145 146 147 /// <summary> 148 /// 获取某一个位置的值 149 /// </summary> 150 /// <param name="listId"></param> 151 /// <param name="index"></param> 152 /// <returns></returns> 153 public string getItemFromList(string listId, int index) 154 { 155 return this.client.GetItemFromList(listId, index); 156 } 157 158 /// <summary> 159 /// 设置并修改指定位置的值 160 /// </summary> 161 /// <param name="listId"></param> 162 /// <param name="index"></param> 163 /// <param name="value"></param> 164 public void setItemInList(string listId, int index,string value) 165 { 166 this.client.SetItemInList(listId, index, value); 167 } 168 169 170 /// <summary> 171 /// 获取列表所有的值 172 /// </summary> 173 /// <param name="listId"></param> 174 /// <returns></returns> 175 public List<string> getAllItemsFromList(string listId) { 176 return this.client.GetAllItemsFromList(listId); 177 } 178 179 /// <summary> 180 /// 删除所有内容 181 /// </summary> 182 /// <param name="listId"></param> 183 public void removeAllFromList(string listId) { 184 this.client.RemoveAllFromList(listId); 185 } 186 187 /// <summary> 188 /// 删除列表指定元素 189 /// </summary> 190 /// <param name="listId"></param> 191 /// <param name="attr"></param> 192 public void removeItemFromList(string listId, string value) { 193 this.client.RemoveItemFromList(listId, value); 194 } 195 196 /// <summary> 197 /// 获取指定列表的长度 198 /// </summary> 199 /// <param name="listId"></param> 200 /// <returns></returns> 201 public long getListCount(string listId) { 202 return this.client.GetListCount(listId); 203 } 204 205 #endregion 206 207 #region Hash 208 209 /// <summary> 210 /// 设置Hash的值 211 /// </summary> 212 /// <param name="hashId"></param> 213 /// <param name="key"></param> 214 /// <param name="value"></param> 215 /// <returns></returns> 216 public bool setEntryInHash(string hashId, string key, string value) 217 { 218 return this.client.SetEntryInHash(hashId, key, value); 219 } 220 221 /// <summary> 222 /// 获取Hash中的值 223 /// </summary> 224 /// <param name="hashId"></param> 225 /// <param name="key"></param> 226 /// <returns></returns> 227 public string getValueFromHash(string hashId, string key) 228 { 229 return this.client.GetValueFromHash(hashId, key); 230 } 231 232 /// <summary> 233 /// 获取Hash列表中的所有内容 234 /// </summary> 235 /// <param name="hashId"></param> 236 public Dictionary<string,string> getAllEntriesFromHash(string hashId) 237 { 238 return this.client.GetAllEntriesFromHash(hashId); 239 } 240 241 /// <summary> 242 /// 判断Hash是否存在指定的键 243 /// </summary> 244 /// <param name="hashId"></param> 245 /// <param name="key"></param> 246 /// <returns></returns> 247 public bool hashContainsEntry(string hashId, string key) 248 { 249 return this.client.HashContainsEntry(hashId, key); 250 } 251 252 /// <summary> 253 /// 设置多个值到Hash 254 /// </summary> 255 /// <param name="hashId"></param> 256 /// <param name="keyValuePairs"></param> 257 public void setRangeInHash(string hashId,Dictionary<string,string> keyValuePairs) { 258 this.client.SetRangeInHash(hashId, keyValuePairs); 259 } 260 261 /// <summary> 262 /// 获取Hash列表的长度 263 /// </summary> 264 /// <param name="hashId"></param> 265 /// <returns></returns> 266 public long getHashCount(string hashId) { 267 return this.client.GetHashCount(hashId); 268 } 269 270 /// <summary> 271 /// 删除某一个值 272 /// </summary> 273 /// <param name="hashId"></param> 274 /// <param name="key"></param> 275 /// <returns></returns> 276 public bool removeEntryFromHash(string hashId,string key) { 277 return this.client.RemoveEntryFromHash(hashId, key); 278 } 279 280 #endregion 281 282 #region Set 283 284 /// <summary> 285 /// 从Set中获取随机值 286 /// </summary> 287 /// <param name="setId"></param> 288 /// <returns></returns> 289 public string getRandomItemFromSet(string setId) 290 { 291 return this.client.GetRandomItemFromSet(setId); 292 } 293 294 /// <summary> 295 /// 获取所有的值 296 /// </summary> 297 /// <param name="setId"></param> 298 /// <returns></returns> 299 public HashSet<string> getAllItemsFromSet(string setId) 300 { 301 return this.client.GetAllItemsFromSet(setId); 302 } 303 304 /// <summary> 305 /// 获取set的长度 306 /// </summary> 307 /// <param name="setId"></param> 308 /// <returns></returns> 309 public long getSetCount(string setId) 310 { 311 return this.client.GetSetCount(setId); 312 } 313 314 /// <summary> 315 /// 删除某一项 316 /// </summary> 317 /// <param name="setId"></param> 318 /// <param name="item"></param> 319 public void removeItemFromSet(string setId,string item) { 320 this.client.RemoveItemFromSet(setId, item); 321 } 322 323 /// <summary> 324 /// 新增内容 325 /// </summary> 326 /// <param name="setId"></param> 327 /// <param name="item"></param> 328 public void addItemToSet(string setId, string item) { 329 this.client.AddItemToSet(setId, item); 330 } 331 332 /// <summary> 333 /// 增加列表到Set 334 /// </summary> 335 /// <param name="setId"></param> 336 /// <param name="items"></param> 337 public void addRangeToSet(string setId,List<string> items) { 338 this.client.AddRangeToSet(setId, items); 339 } 340 341 #endregion 342 343 #region zset 344 345 /// <summary> 346 /// 添加元素到排序集合 347 /// </summary> 348 /// <param name="setId"></param> 349 /// <param name="value"></param> 350 /// <returns></returns> 351 public bool addItemToSortedSet(string setId,string value) { 352 return this.client.AddItemToSortedSet(setId, value); 353 } 354 355 /// <summary> 356 /// 添加元素到排序集合 357 /// </summary> 358 /// <param name="setId"></param> 359 /// <param name="value"></param> 360 /// <param name="score"></param> 361 /// <returns></returns> 362 public bool addItemToSortedSet(string setId, string value,double score) 363 { 364 return this.client.AddItemToSortedSet(setId, value,score); 365 } 366 367 /// <summary> 368 /// 增加列表到排序集合 369 /// </summary> 370 /// <param name="setId"></param> 371 /// <param name="values"></param> 372 /// <param name="score"></param> 373 /// <returns></returns> 374 public bool addRangeToSortedSet(string setId,List<string> values, double score) { 375 return this.client.AddRangeToSortedSet(setId, values, score); 376 } 377 378 /// <summary> 379 /// 增加列表到排序集合 380 /// </summary> 381 /// <param name="setId"></param> 382 /// <param name="values"></param> 383 /// <param name="score"></param> 384 /// <returns></returns> 385 public bool addRangeToSortedSet(string setId, List<string> values, long score) 386 { 387 return this.client.AddRangeToSortedSet(setId, values, score); 388 } 389 390 /// <summary> 391 /// 获取所有的集合内容 392 /// </summary> 393 /// <param name="setId"></param> 394 /// <returns></returns> 395 public List<string> getAllItemsFromSortedSet(string setId) { 396 return this.client.GetAllItemsFromSortedSet(setId); 397 } 398 399 /// <summary> 400 /// 倒序获取所有的内容 401 /// </summary> 402 /// <param name="setId"></param> 403 /// <returns></returns> 404 public List<string> getAllItemsFromSortedSetDesc(string setId) { 405 return this.client.GetAllItemsFromSortedSetDesc(setId); 406 } 407 408 /// <summary> 409 /// 带分数一起取出 410 /// </summary> 411 /// <param name="setId"></param> 412 /// <returns></returns> 413 public IDictionary<string, double> getAllWithScoresFromSortedSet(string setId) 414 { 415 return this.client.GetAllWithScoresFromSortedSet(setId); 416 } 417 418 /// <summary> 419 /// 获取对应值的位置 420 /// </summary> 421 /// <param name="setId"></param> 422 /// <param name="value"></param> 423 /// <returns></returns> 424 public long getItemIndexInSortedSet(string setId,string value) 425 { 426 return this.client.GetItemIndexInSortedSet(setId, value); 427 } 428 429 /// <summary> 430 /// 倒序的位置 431 /// </summary> 432 /// <param name="setId"></param> 433 /// <param name="value"></param> 434 /// <returns></returns> 435 public long getItemIndexInSortedSetDesc(string setId, string value) { 436 return this.client.GetItemIndexInSortedSetDesc(setId, value); 437 } 438 439 /// <summary> 440 /// 获取对应元素的分数 441 /// </summary> 442 /// <param name="setId"></param> 443 /// <param name="value"></param> 444 /// <returns></returns> 445 public double getItemScoreInSortedSet(string setId, string value) { 446 return this.client.GetItemScoreInSortedSet(setId, value); 447 } 448 449 /// <summary> 450 /// 451 /// </summary> 452 /// <param name="setId"></param> 453 /// <param name="fromRank"></param> 454 /// <param name="toRank"></param> 455 /// <returns></returns> 456 public List<string> getRangeFromSortedSet(string setId, int fromRank, int toRank) { 457 return this.client.GetRangeFromSortedSet(setId, fromRank, toRank); 458 } 459 460 public IDictionary<string, double> getRangeWithScoresFromSortedSet(string setId, int fromRank, int toRank) { 461 return this.client.GetRangeWithScoresFromSortedSet(setId, fromRank, toRank); 462 } 463 464 public void Dispose() 465 { 466 if(this.client != null) 467 { 468 this.client.Dispose(); 469 } 470 } 471 #endregion 472 } 473 }