You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.4KB

  1. using GpsCardGatewayPosition.Model.Config;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using TelpoDataService.Util.Clients;
  10. using TelpoDataService.Util.Entities.GpsCard;
  11. using TelpoDataService.Util.Models;
  12. using TelpoDataService.Util.QueryObjects;
  13. using TelpoDataService.Util;
  14. using GpsCardGatewayPosition.Model.Cache;
  15. namespace GpsCardGatewayPosition.Service.Cache
  16. {
  17. public class PersonCacheManager
  18. {
  19. private const int DEFAULT_DURATION_SECONDS = 1200; //20分钟
  20. private const string CACHE_KEY_PERSON = "Person_";
  21. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  22. private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH";
  23. private readonly ServiceConfig _configService;
  24. private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
  25. private readonly ILogger<PersonCacheManager> _logger;
  26. private readonly IOptions<RedisConfig> _optConfigRedis;
  27. public PersonCacheManager(IOptions<ServiceConfig> optConfigService, GpsCardAccessorClient<GpsPerson> personApiClient,
  28. ILogger<PersonCacheManager> logger, IOptions<RedisConfig> optConfigRedis)
  29. {
  30. _configService = optConfigService.Value;
  31. _optConfigRedis = optConfigRedis;
  32. _personApiClient = personApiClient;
  33. _logger = logger;
  34. }
  35. public async Task<GpsPerson> GetPersonBySerialNoAsync(string messageId, string sn)
  36. {
  37. string key = CACHE_KEY_PERSON + sn;
  38. var person = await RedisHelper.GetAsync<GpsPerson>(key).ConfigureAwait(false);
  39. if (person == null)
  40. {
  41. try
  42. {
  43. string url = _configService.TelpoDataUrl;
  44. var param = new GeneralParam
  45. {
  46. Filters = new List<QueryFilterCondition>
  47. {
  48. new QueryFilterCondition
  49. {
  50. Key=nameof(GpsPerson.Serialno),
  51. Value=sn,
  52. ValueType=QueryValueTypeEnum.String,
  53. Operator=QueryOperatorEnum.Equal
  54. }
  55. }
  56. };
  57. person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  58. if (person != null)
  59. {
  60. RedisHelper.SetAsync(key, person, DEFAULT_DURATION_SECONDS);
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. _logger.LogError($"获取查询使用者并更新缓存发生异常:{ex.Message}, {ex.StackTrace}");
  66. }
  67. }
  68. return person;
  69. }
  70. /// <summary>
  71. /// 读取个人信息(血压使用)注意要改用 redis DB7
  72. /// </summary>
  73. /// <param name="messageId"></param>
  74. /// <param name="sn"></param>
  75. /// <returns></returns>
  76. public async Task<GpsDevicePerson> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
  77. {
  78. if (string.IsNullOrWhiteSpace(sn)) return null;
  79. // 切换redis DB7 数据库和前缀 "TELPO"
  80. //_optConfigRedis.Value.DefaultDatabase = 7;
  81. //_optConfigRedis.Value.Prefix = "TELPO";
  82. // 增加容错,防止Redis宕机造成业务中断
  83. try
  84. {
  85. //using (var csRedisDb7 = new CSRedisClient(_optConfigRedis.Value.ToString()))
  86. //{
  87. // var person = await csRedisDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
  88. // return person;
  89. //}
  90. var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
  91. return person;
  92. }
  93. catch (Exception ex)
  94. {
  95. _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  96. var param = new GeneralParam
  97. {
  98. Filters = new List<QueryFilterCondition>
  99. {
  100. new QueryFilterCondition
  101. {
  102. Key=nameof(GpsPerson.Serialno),
  103. Value=sn,
  104. ValueType=QueryValueTypeEnum.String,
  105. Operator=QueryOperatorEnum.Equal
  106. }
  107. }
  108. };
  109. var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  110. // 读取数据库,构造新实例
  111. return new GpsDevicePerson
  112. {
  113. Time = DateTime.Now,
  114. Person = new Person
  115. {
  116. DeviceId = person.DeviceId,
  117. PersonId = person.PersonId,
  118. Remarks = person.Remarks,
  119. SerialNo = person.Serialno
  120. }
  121. };
  122. }
  123. }
  124. }
  125. }