定位推送服务
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

209 lines
9.5KB

  1. using Microsoft.Extensions.Options;
  2. using TelpoPush.Position.Worker.Models.CacheTemplates;
  3. using TelpoPush.Position.Worker.Models.Config;
  4. namespace TelpoPush.Position.Worker.Service.Cache
  5. {
  6. public class RedisUtil
  7. {
  8. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE = "TELPO#GPSDEVICE_HASH";
  9. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PERSON = "TELPO#GPSDEVICE_PERSON_HASH";
  10. private const string CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG = "TELPO#MANUFACTOR_CONFG_HASH";
  11. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS = "TELPO#GPSDEVICE_PUSH_SITTINGS_HASH";
  12. private readonly ILogger<RedisUtil> _logger;
  13. private readonly ServiceConfig _configService;
  14. private readonly SqlMapper _sqlMapper;
  15. public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
  16. {
  17. _configService = configService.Value;
  18. _logger = logger;
  19. optConfigRedis.Value.Prefix = "";
  20. optConfigRedis.Value.DefaultDatabase = 7;
  21. var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
  22. RedisHelper.Initialization(csredis);
  23. _sqlMapper = sqlMapper;
  24. }
  25. public async Task<DeviceInfoModel> GetGpsDevice(string imei)
  26. {
  27. if (string.IsNullOrWhiteSpace(imei)) return null;
  28. string keyCache = $"{imei}_GpsDevice";
  29. try
  30. {
  31. var objCache = MemoryCacheUtil.Get<DeviceInfoModel>(keyCache);
  32. if (objCache == null)
  33. {
  34. var obj = await RedisHelper.HGetAsync<DeviceInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei);
  35. if (obj == null)
  36. {
  37. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  38. if (deviceInfo != null)
  39. {
  40. RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  41. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
  42. return deviceInfo;
  43. }
  44. }
  45. else
  46. {
  47. MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
  48. return obj;
  49. }
  50. }
  51. return objCache;
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.LogError($"GetGpsDevice,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  56. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  57. if (deviceInfo != null)
  58. {
  59. RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  60. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
  61. return deviceInfo;
  62. }
  63. }
  64. return null;
  65. }
  66. public async Task<PersonInfoModel> SetPersonInfoHash(string imei)
  67. {
  68. if (string.IsNullOrWhiteSpace(imei)) return null;
  69. string keyCache = $"{imei}_PersonInfoHash";
  70. var personInfo = _sqlMapper.PersonInfo(imei);
  71. PersonInfoModel model = new PersonInfoModel()
  72. {
  73. person = personInfo,
  74. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  75. };
  76. if (personInfo != null)
  77. {
  78. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE_PERSON, imei, model);
  79. MemoryCacheUtil.Set(keyCache, model, 60);//1分钟
  80. }
  81. else
  82. {
  83. await RedisHelper.HDelAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE_PERSON, imei);
  84. MemoryCacheUtil.Remove(keyCache);
  85. }
  86. return model;
  87. }
  88. public async Task<string> GetHealthyDeviceKey(string imei)
  89. {
  90. if (string.IsNullOrWhiteSpace(imei)) return null;
  91. string HealthyDeviceKey = $"Telpol:HealthyDeviceKey:{imei}";
  92. string keyCache = $"{imei}_HealthyDeviceKey";
  93. try
  94. {
  95. var objCache = MemoryCacheUtil.Get<string>(keyCache);
  96. if (objCache == null)
  97. {
  98. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  99. if (!string.IsNullOrEmpty(deviceKey))
  100. {
  101. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  102. return deviceKey;
  103. }
  104. }
  105. return objCache;
  106. }
  107. catch (Exception ex)
  108. {
  109. _logger.LogError($"GetHealthyDeviceKey,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  110. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  111. if (!string.IsNullOrEmpty(deviceKey))
  112. {
  113. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  114. return deviceKey;
  115. }
  116. else
  117. return "";
  118. }
  119. }
  120. public async Task<SettingInfosItem> GetManufactorPushSettingHash(string imei, string manufactorId, int dataType)
  121. {
  122. if (string.IsNullOrWhiteSpace(manufactorId)) return null;
  123. string keyCache = $"{manufactorId}_{dataType}_ManufactorPushSettingHash";
  124. SettingInfosItem settingInfos = null;
  125. try
  126. {
  127. var objCache = MemoryCacheUtil.Get<SettingInfosItem>(keyCache);
  128. if (objCache == null)
  129. {
  130. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  131. if (obj != null)
  132. {
  133. if (obj.pushSettings.Any())
  134. {
  135. DateTime dt = DateTime.Now;
  136. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  137. if (settingsItem != null)
  138. {
  139. if (settingsItem.settingInfos.Any())
  140. {
  141. foreach (var item in settingsItem.settingInfos)
  142. {
  143. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  144. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  145. if (item.pushFlag && (dt > startTime && dt < endTime))
  146. {
  147. settingInfos = item;
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if (settingInfos != null)
  155. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  156. else
  157. MemoryCacheUtil.Remove(keyCache);
  158. }
  159. }
  160. else
  161. settingInfos = objCache;
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.LogError($"GetManufactorPushSettingHash(imei={imei},dataType={dataType}),key={manufactorId},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  166. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  167. if (obj != null)
  168. {
  169. if (obj.pushSettings.Any())
  170. {
  171. DateTime dt = DateTime.Now;
  172. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  173. if (settingsItem != null)
  174. {
  175. if (settingsItem.settingInfos.Any())
  176. {
  177. foreach (var item in settingsItem.settingInfos)
  178. {
  179. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  180. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  181. if (item.pushFlag && (dt > startTime && dt < endTime))
  182. {
  183. settingInfos = item;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. if (settingInfos != null)
  191. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  192. else
  193. MemoryCacheUtil.Remove(keyCache);
  194. }
  195. }
  196. return settingInfos;
  197. }
  198. }
  199. }