RedisHashHelperAsync.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using OASystem.RedisRepository.CommonHelper;
  2. using StackExchange.Redis;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OASystem.RedisRepository.RedisAsyncHelper
  9. {
  10. /// <summary>
  11. /// Redis异步操作类 Hash部分类
  12. /// </summary>
  13. internal partial class RedisHelper //: IRedisHelper
  14. {
  15. #region Hash 写操作
  16. public async Task<bool> HashSetAsync<T>(string key, string field, T value)
  17. {
  18. return await _client.HashSetAsync(key, field, SerializeHelper.Serialize(value));
  19. }
  20. public async Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields)
  21. {
  22. List<HashEntry> entries = new List<HashEntry>();
  23. hashFields.ToList().ForEach(d => entries.Add(new HashEntry(d.Key, SerializeHelper.Serialize(d.Value))));
  24. await _client.HashSetAsync(key, entries.ToArray());
  25. }
  26. public async Task<long> HashIncrementAsync(string key, string field, long incrCount = 1)
  27. {
  28. return await _client.HashIncrementAsync(key, field, incrCount);
  29. }
  30. public async Task<long> HashDecrementAsync(string key, string field, long decrCount = 1)
  31. {
  32. return await _client.HashDecrementAsync(key, field, decrCount);
  33. }
  34. public async Task<bool> HashDeleteFieldAsync(string key, string field)
  35. {
  36. return await _client.HashDeleteAsync(key, field);
  37. }
  38. public async Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields)
  39. {
  40. List<RedisValue> values = new List<RedisValue>();
  41. fields.ForEach(f => values.Add(f));
  42. return await _client.HashDeleteAsync(key, values.ToArray());
  43. }
  44. #endregion
  45. #region Hash 读操作
  46. /// <summary>
  47. /// Redis 指定hash类型key中field是否存在
  48. /// </summary>
  49. /// <param name="key"></param>
  50. /// <param name="field"></param>
  51. /// <returns></returns>
  52. public async Task<bool> HashFieldExistAsync(string key, string field)
  53. {
  54. return await _client.HashExistsAsync(key, field, CommandFlags.PreferSlave);
  55. }
  56. public async Task<List<string>> HashFieldsAsync(string key)
  57. {
  58. RedisValue[] values = await _client.HashKeysAsync(key, CommandFlags.PreferSlave);
  59. return RedisInnerTypeHelper.RedisValuesToGenericList<string>(values);
  60. }
  61. public async Task<List<T>> HashValuesAsync<T>(string key)
  62. {
  63. var values = await _client.HashValuesAsync(key, CommandFlags.PreferSlave);
  64. return RedisInnerTypeHelper.RedisValuesToGenericList<T>(values);
  65. }
  66. public async Task<T> HashGetAsync<T>(string key, string field)
  67. {
  68. return SerializeHelper.Deserialize<T>(await _client.HashGetAsync(key, field, CommandFlags.PreferSlave));
  69. }
  70. public async Task<Dictionary<string, T>> HashGetAllAsync<T>(string key)
  71. {
  72. HashEntry[] entries = await _client.HashGetAllAsync(key, CommandFlags.PreferSlave);
  73. Dictionary<string, T> dic = new Dictionary<string, T>();
  74. entries.ToList().ForEach(e => dic.Add(e.Name, SerializeHelper.Deserialize<T>(e.Value)));
  75. return dic;
  76. }
  77. #endregion
  78. }
  79. }