PersonCacheManager.cs 6.1KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using CSRedis;
  2. using HealthMonitor.Model.Cache;
  3. using HealthMonitor.Model.Config;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Options;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Net.NetworkInformation;
  11. using System.Threading.Tasks;
  12. using TelpoDataService.Util;
  13. using TelpoDataService.Util.Clients;
  14. using TelpoDataService.Util.Entities.GpsCard;
  15. using TelpoDataService.Util.Models;
  16. using TelpoDataService.Util.QueryObjects;
  17. namespace HealthMonitor.Service.Cache
  18. {
  19. public class PersonCacheManager
  20. {
  21. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  22. private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH";
  23. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  24. private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
  25. private readonly ILogger<PersonCacheManager> _logger;
  26. public PersonCacheManager(
  27. GpsCardAccessorClient<GpsPerson> personApiClient,
  28. ILogger<PersonCacheManager> logger)
  29. {
  30. _personApiClient = personApiClient;
  31. _logger = logger;
  32. }
  33. //public async Task<GpsPerson?> GetPersonBySerialNoAsync(string messageId, string sn)
  34. //{
  35. // if (string.IsNullOrWhiteSpace(sn)) return null;
  36. // // 切换redis DB7 数据库和前缀 "TELPO"
  37. // // 增加容错,防止Redis宕机造成业务中断
  38. // try
  39. // {
  40. // return await csRedisDb7.HGetAsync<GpsPerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
  41. // }
  42. // catch (Exception ex)
  43. // {
  44. // _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  45. // var param = new GeneralParam
  46. // {
  47. // Filters = new List<QueryFilterCondition>
  48. // {
  49. // new QueryFilterCondition
  50. // {
  51. // Key=nameof(GpsPerson.Serialno),
  52. // Value=sn,
  53. // ValueType=QueryValueTypeEnum.String,
  54. // Operator=QueryOperatorEnum.Equal
  55. // }
  56. // }
  57. // }!;
  58. // return await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  59. // }
  60. //}
  61. /// <summary>
  62. /// 读取个人信息(血压使用)注意要改用 redis DB7
  63. /// </summary>
  64. /// <param name="messageId"></param>
  65. /// <param name="sn"></param>
  66. /// <returns></returns>
  67. public async Task<GpsDevicePerson?> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
  68. {
  69. if (string.IsNullOrWhiteSpace(sn)) return null;
  70. // 增加容错,防止Redis宕机造成业务中断
  71. try
  72. {
  73. var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
  74. return person;
  75. }
  76. catch (Exception ex)
  77. {
  78. _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  79. var param = new GeneralParam
  80. {
  81. Filters = new List<QueryFilterCondition>
  82. {
  83. new QueryFilterCondition
  84. {
  85. Key=nameof(GpsPerson.Serialno),
  86. Value=sn,
  87. ValueType=QueryValueTypeEnum.String,
  88. Operator=QueryOperatorEnum.Equal
  89. }
  90. }
  91. }!;
  92. var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  93. // 读取数据库,构造新实例
  94. return new GpsDevicePerson {
  95. Time = DateTime.Now,
  96. Person = new Person {
  97. DeviceId = person.DeviceId,
  98. PersonId = person.PersonId,
  99. Remarks = person.Remarks,
  100. SerialNo = person.Serialno
  101. }
  102. };
  103. }
  104. }
  105. /// <summary>
  106. /// 获取gps_person缓存
  107. /// </summary>
  108. /// <param name="messageId"></param>
  109. /// <param name="sn"></param>
  110. /// <returns></returns>
  111. public async Task<JObject?> GetDeviceGpsPersonCacheObjectBySerialNoAsync(string messageId, string sn)
  112. {
  113. if (string.IsNullOrWhiteSpace(sn)) return null;
  114. try
  115. {
  116. var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
  117. return (JObject?)JsonConvert.DeserializeObject(person);
  118. }
  119. catch (Exception ex)
  120. {
  121. _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  122. }
  123. return null;
  124. }
  125. /// <summary>
  126. /// 更新gps_person缓存
  127. /// </summary>
  128. /// <param name="person"></param>
  129. /// <param name="sn"></param>
  130. /// <returns></returns>
  131. public async Task<bool> UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(JObject person, string sn)
  132. {
  133. var flag = false;
  134. if (string.IsNullOrWhiteSpace(sn)) return flag;
  135. try
  136. {
  137. // 覆盖 返回false
  138. await RedisHelperDb7.HSetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn, person).ConfigureAwait(false);
  139. flag = true;
  140. }
  141. catch (Exception ex)
  142. {
  143. _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  144. }
  145. return flag;
  146. }
  147. }
  148. }