RedisStringHelperAsync.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using OASystem.RedisRepository.CommonHelper;
  2. using OASystem.RedisRepository.Config;
  3. using StackExchange.Redis;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OASystem.RedisRepository.RedisAsyncHelper
  10. {/// <summary>
  11. /// Redis异步操作类 String部分类
  12. /// </summary>
  13. internal partial class RedisHelper : IRedisHelper
  14. {
  15. private static IDatabase _client;
  16. internal RedisHelper()
  17. {
  18. _client = RedisConnection.GenerateConnection.GetDatabase();
  19. }
  20. #region String 写操作
  21. /// <summary>
  22. /// 将任何数据添加到redis中
  23. /// </summary>
  24. /// <typeparam name="T"></typeparam>
  25. /// <param name="key"></param>
  26. /// <param name="value"></param>
  27. /// <param name="timeout"></param>
  28. /// <returns></returns>
  29. public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null)
  30. {
  31. return await _client.StringSetAsync(key, SerializeHelper.Serialize(value), timeout);
  32. }
  33. public async Task<long> StringDecrementAsync(string key, long value = 1L)
  34. {
  35. return await _client.StringDecrementAsync(key, value);
  36. }
  37. public async Task<long> StringIncrementAsync(string key, long value = 1L)
  38. {
  39. return await _client.StringIncrementAsync(key, value);
  40. }
  41. #endregion
  42. #region String 读操作
  43. /// <summary>
  44. /// 根据key获取指定类型数据
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="key"></param>
  48. /// <returns></returns>
  49. public async Task<T> StringGetAsync<T>(string key)
  50. {
  51. return SerializeHelper.Deserialize<T>(await _client.StringGetAsync(key, CommandFlags.PreferSlave));
  52. }
  53. #endregion
  54. }
  55. }