選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

121 行
4.2KB

  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 System;
  7. using System.Collections.Generic;
  8. using System.Net.NetworkInformation;
  9. using System.Threading.Tasks;
  10. using TelpoDataService.Util;
  11. using TelpoDataService.Util.Clients;
  12. using TelpoDataService.Util.Entities.GpsCard;
  13. using TelpoDataService.Util.Models;
  14. using TelpoDataService.Util.QueryObjects;
  15. namespace HealthMonitor.Service.Cache
  16. {
  17. public class PersonCacheManager
  18. {
  19. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  20. private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH";
  21. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  22. private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
  23. private readonly ILogger<PersonCacheManager> _logger;
  24. public PersonCacheManager(
  25. GpsCardAccessorClient<GpsPerson> personApiClient,
  26. ILogger<PersonCacheManager> logger)
  27. {
  28. _personApiClient = personApiClient;
  29. _logger = logger;
  30. }
  31. //public async Task<GpsPerson?> GetPersonBySerialNoAsync(string messageId, string sn)
  32. //{
  33. // if (string.IsNullOrWhiteSpace(sn)) return null;
  34. // // 切换redis DB7 数据库和前缀 "TELPO"
  35. // // 增加容错,防止Redis宕机造成业务中断
  36. // try
  37. // {
  38. // return await csRedisDb7.HGetAsync<GpsPerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
  39. // }
  40. // catch (Exception ex)
  41. // {
  42. // _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  43. // var param = new GeneralParam
  44. // {
  45. // Filters = new List<QueryFilterCondition>
  46. // {
  47. // new QueryFilterCondition
  48. // {
  49. // Key=nameof(GpsPerson.Serialno),
  50. // Value=sn,
  51. // ValueType=QueryValueTypeEnum.String,
  52. // Operator=QueryOperatorEnum.Equal
  53. // }
  54. // }
  55. // }!;
  56. // return await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  57. // }
  58. //}
  59. /// <summary>
  60. /// 读取个人信息(血压使用)注意要改用 redis DB7
  61. /// </summary>
  62. /// <param name="messageId"></param>
  63. /// <param name="sn"></param>
  64. /// <returns></returns>
  65. public async Task<GpsDevicePerson?> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
  66. {
  67. if (string.IsNullOrWhiteSpace(sn)) return null;
  68. // 增加容错,防止Redis宕机造成业务中断
  69. try
  70. {
  71. var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
  72. return person;
  73. }
  74. catch(Exception ex)
  75. {
  76. _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  77. var param = new GeneralParam
  78. {
  79. Filters = new List<QueryFilterCondition>
  80. {
  81. new QueryFilterCondition
  82. {
  83. Key=nameof(GpsPerson.Serialno),
  84. Value=sn,
  85. ValueType=QueryValueTypeEnum.String,
  86. Operator=QueryOperatorEnum.Equal
  87. }
  88. }
  89. }!;
  90. var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  91. // 读取数据库,构造新实例
  92. return new GpsDevicePerson {
  93. Time = DateTime.Now,
  94. Person=new Person {
  95. DeviceId=person.DeviceId,
  96. PersonId=person.PersonId,
  97. Remarks=person.Remarks,
  98. SerialNo=person.Serialno
  99. }
  100. };
  101. }
  102. }
  103. }
  104. }