您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

135 行
5.7KB

  1. using Microsoft.Extensions.Logging;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TelpoPush.Models.CacheTemplates;
  9. using TelpoPush.Models.Config;
  10. namespace TelpoPush.Service.Cache
  11. {
  12. public class RedisUtil
  13. {
  14. private const string CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG = "TELPO#MANUFACTOR_CONFG_HASH";
  15. private readonly ILogger<RedisUtil> _logger;
  16. private readonly ServiceConfig _configService;
  17. private readonly SqlMapper _sqlMapper;
  18. public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
  19. {
  20. _configService = configService.Value;
  21. _logger = logger;
  22. optConfigRedis.Value.Prefix = "";
  23. optConfigRedis.Value.DefaultDatabase = 7;
  24. var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
  25. RedisHelper.Initialization(csredis);
  26. _sqlMapper = sqlMapper;
  27. }
  28. public async Task<string> GetHealthyDeviceKey(string imei)
  29. {
  30. if (string.IsNullOrWhiteSpace(imei)) return null;
  31. string HealthyDeviceKey = $"Telpol:HealthyDeviceKey:{imei}";
  32. string keyCache = $"{imei}_HealthyDeviceKey";
  33. try
  34. {
  35. var objCache = MemoryCacheUtil.Get<string>(keyCache);
  36. if (objCache == null)
  37. {
  38. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  39. if (!string.IsNullOrEmpty(deviceKey))
  40. {
  41. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  42. return deviceKey;
  43. }
  44. }
  45. return objCache;
  46. }
  47. catch (Exception ex)
  48. {
  49. _logger.LogError($"GetHealthyDeviceKey,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  50. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  51. if (!string.IsNullOrEmpty(deviceKey))
  52. {
  53. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  54. return deviceKey;
  55. }
  56. else
  57. return "";
  58. }
  59. }
  60. //public async Task<string> ManufactorKafkaTopicQuery()
  61. //{
  62. // List<string> topics = new List<string>();
  63. // string keyCache = $"ManufactorKafkaConfig";
  64. // try
  65. // {
  66. // var objCache = MemoryCacheUtil.Get<List<string>>(keyCache);
  67. // if (objCache == null)
  68. // {
  69. // var obj = await RedisHelper.HGetAsync<List<ManufactorKafkaModel>>(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei);
  70. // if (obj == null)
  71. // {
  72. // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  73. // if (manufactors != null)
  74. // {
  75. // foreach (var item in manufactors) {
  76. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  77. // topics.Add(item.kafkaTopic);
  78. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  79. // }
  80. // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
  81. // return topics;
  82. // }
  83. // }
  84. // else
  85. // {
  86. // foreach (var item in obj)
  87. // {
  88. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  89. // topics.Add(item.kafkaTopic);
  90. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  91. // }
  92. // MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
  93. // return topics;
  94. // }
  95. // }
  96. // return objCache;
  97. // }
  98. // catch (Exception ex)
  99. // {
  100. // _logger.LogError($"ManufactorKafkaTopicQuery,缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  101. // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  102. // if (manufactors != null)
  103. // {
  104. // foreach (var item in manufactors)
  105. // {
  106. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  107. // topics.Add(item.kafkaTopic);
  108. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  109. // }
  110. // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
  111. // return topics;
  112. // }
  113. // //var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  114. // //if (manufactors != null)
  115. // //{
  116. // // RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei, manufactor);
  117. // // RedisHelper.Set(keyCache, manufactor, _configService.CacheDurationSeconds10);
  118. // // return manufactor;
  119. // //}
  120. // }
  121. // return null;
  122. //}
  123. }
  124. }