Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

6 месяцев назад
6 месяцев назад
6 месяцев назад
6 месяцев назад
6 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS = "TELPO#GPSDEVICE_PUSH_SITTINGS_HASH";
  16. private readonly ILogger<RedisUtil> _logger;
  17. private readonly ServiceConfig _configService;
  18. private readonly SqlMapper _sqlMapper;
  19. public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
  20. {
  21. _configService = configService.Value;
  22. _logger = logger;
  23. optConfigRedis.Value.Prefix = "";
  24. optConfigRedis.Value.DefaultDatabase = 7;
  25. var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
  26. RedisHelper.Initialization(csredis);
  27. _sqlMapper = sqlMapper;
  28. }
  29. public async Task<string> GetHealthyDeviceKey(string imei)
  30. {
  31. if (string.IsNullOrWhiteSpace(imei)) return null;
  32. string HealthyDeviceKey = $"Telpol:HealthyDeviceKey:{imei}";
  33. string keyCache = $"{imei}_HealthyDeviceKey";
  34. try
  35. {
  36. var objCache = MemoryCacheUtil.Get<string>(keyCache);
  37. if (objCache == null)
  38. {
  39. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  40. if (!string.IsNullOrEmpty(deviceKey))
  41. {
  42. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  43. return deviceKey;
  44. }
  45. }
  46. return objCache;
  47. }
  48. catch (Exception ex)
  49. {
  50. _logger.LogError($"GetHealthyDeviceKey,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  51. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  52. if (!string.IsNullOrEmpty(deviceKey))
  53. {
  54. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  55. return deviceKey;
  56. }
  57. else
  58. return "";
  59. }
  60. }
  61. public async Task<SettingInfosItem> GetManufactorPushSettingHash(string imei, string manufactorId, int dataType)
  62. {
  63. if (string.IsNullOrWhiteSpace(manufactorId)) return null;
  64. string keyCache = $"{manufactorId}_{dataType}_ManufactorPushSettingHash";
  65. SettingInfosItem settingInfos = null;
  66. try
  67. {
  68. var objCache = MemoryCacheUtil.Get<SettingInfosItem>(keyCache);
  69. if (objCache == null)
  70. {
  71. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  72. if (obj != null)
  73. {
  74. if (obj.pushSettings.Any())
  75. {
  76. DateTime dt = DateTime.Now;
  77. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  78. if (settingsItem != null)
  79. {
  80. if (settingsItem.settingInfos.Any())
  81. {
  82. foreach (var item in settingsItem.settingInfos)
  83. {
  84. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  85. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  86. if (item.pushFlag && (dt > startTime && dt < endTime))
  87. {
  88. settingInfos = item;
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. if (settingInfos != null)
  96. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  97. else
  98. MemoryCacheUtil.Remove(keyCache);
  99. }
  100. }
  101. else
  102. settingInfos = objCache;
  103. }
  104. catch (Exception ex)
  105. {
  106. _logger.LogError($"GetManufactorPushSettingHash(imei={imei},dataType={dataType}),key={manufactorId},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  107. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  108. if (obj != null)
  109. {
  110. if (obj.pushSettings.Any())
  111. {
  112. DateTime dt = DateTime.Now;
  113. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  114. if (settingsItem != null)
  115. {
  116. if (settingsItem.settingInfos.Any())
  117. {
  118. foreach (var item in settingsItem.settingInfos)
  119. {
  120. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  121. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  122. if (item.pushFlag && (dt > startTime && dt < endTime))
  123. {
  124. settingInfos = item;
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. if (settingInfos != null)
  132. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  133. else
  134. MemoryCacheUtil.Remove(keyCache);
  135. }
  136. }
  137. return settingInfos;
  138. }
  139. //public async Task<string> ManufactorKafkaTopicQuery()
  140. //{
  141. // List<string> topics = new List<string>();
  142. // string keyCache = $"ManufactorKafkaConfig";
  143. // try
  144. // {
  145. // var objCache = MemoryCacheUtil.Get<List<string>>(keyCache);
  146. // if (objCache == null)
  147. // {
  148. // var obj = await RedisHelper.HGetAsync<List<ManufactorKafkaModel>>(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei);
  149. // if (obj == null)
  150. // {
  151. // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  152. // if (manufactors != null)
  153. // {
  154. // foreach (var item in manufactors) {
  155. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  156. // topics.Add(item.kafkaTopic);
  157. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  158. // }
  159. // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
  160. // return topics;
  161. // }
  162. // }
  163. // else
  164. // {
  165. // foreach (var item in obj)
  166. // {
  167. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  168. // topics.Add(item.kafkaTopic);
  169. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  170. // }
  171. // MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
  172. // return topics;
  173. // }
  174. // }
  175. // return objCache;
  176. // }
  177. // catch (Exception ex)
  178. // {
  179. // _logger.LogError($"ManufactorKafkaTopicQuery,缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  180. // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  181. // if (manufactors != null)
  182. // {
  183. // foreach (var item in manufactors)
  184. // {
  185. // if (!string.IsNullOrEmpty(item.kafkaTopic))
  186. // topics.Add(item.kafkaTopic);
  187. // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
  188. // }
  189. // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
  190. // return topics;
  191. // }
  192. // //var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
  193. // //if (manufactors != null)
  194. // //{
  195. // // RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei, manufactor);
  196. // // RedisHelper.Set(keyCache, manufactor, _configService.CacheDurationSeconds10);
  197. // // return manufactor;
  198. // //}
  199. // }
  200. // return null;
  201. //}
  202. }
  203. }