您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

130 行
4.2KB

  1. using HealthMonitor.Core.Dal;
  2. using HealthMonitor.Core.Query;
  3. using HealthMonitor.Core.Query.Extensions;
  4. using HealthMonitor.Util.Entities.HealthMonitor;
  5. using Microsoft.EntityFrameworkCore;
  6. using Microsoft.Extensions.Logging;
  7. using Newtonsoft.Json;
  8. using System.Reflection;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. namespace HealthMonitor.Service.Cache
  12. {
  13. public class BloodPressReferenceValueCacheManager
  14. {
  15. private readonly ILogger<BloodPressReferenceValueCacheManager> _logger;
  16. protected readonly IHealthMonitorDataAccessor _dataAccessor;
  17. private static readonly object _syncLocker = new();
  18. private readonly MD5 _md5;
  19. private const string CACHE_KEY_BP_REF_VALUE = "HM_REF_Val_";
  20. public BloodPressReferenceValueCacheManager(
  21. IHealthMonitorDataAccessor dataAccessor,
  22. ILogger<BloodPressReferenceValueCacheManager> logger
  23. )
  24. {
  25. _md5 = MD5.Create();
  26. _dataAccessor = dataAccessor;
  27. _logger = logger;
  28. }
  29. public async Task<HmBloodPressReferenceValue> GetBloodPressReferenceValueAsync(int age, int gender,bool isHypertension)
  30. {
  31. var hashModel = new
  32. {
  33. Index = $"{age}-{gender}-{isHypertension}"
  34. };
  35. var hash = ComputeHash(hashModel);
  36. var cacheKey = CACHE_KEY_BP_REF_VALUE + $"{hash}";
  37. var bpRef = await RedisHelper.GetAsync<HmBloodPressReferenceValue>(cacheKey).ConfigureAwait(false);
  38. if (bpRef == null)
  39. {
  40. try
  41. {
  42. bpRef = await _dataAccessor
  43. .GetFirstOrDefaultAsync<HmBloodPressReferenceValue>
  44. (
  45. i =>
  46. i.Age.Equals(age) &&
  47. i.Gender.Equals(gender) &&
  48. i.Hypertension.Equals(isHypertension)
  49. ).ConfigureAwait(false);
  50. await RedisHelper.SetAsync(cacheKey, JsonConvert.SerializeObject(bpRef));
  51. return bpRef;
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.LogError($"{nameof(GetBloodPressReferenceValueAsync)}发生异常:{ex.Message}, {ex.StackTrace}");
  56. return new HmBloodPressReferenceValue
  57. {
  58. Age = 0,
  59. };
  60. }
  61. }
  62. return bpRef;
  63. }
  64. public async Task UpdateBloodPressReferenceValueAsync(HmBloodPressReferenceValue bpRef)
  65. {
  66. try
  67. {
  68. var hashModel = new
  69. {
  70. Index = $"{bpRef.Age}-{bpRef.Gender}-{bpRef.Hypertension}"
  71. };
  72. var hash = ComputeHash(hashModel);
  73. var cacheKey = CACHE_KEY_BP_REF_VALUE + $"{hash}";
  74. await RedisHelper.SetAsync(cacheKey, JsonConvert.SerializeObject(bpRef));
  75. }
  76. catch (Exception ex)
  77. {
  78. _logger.LogError($"{nameof(UpdateBloodPressReferenceValueAsync)}发生异常:{ex.Message}, {ex.StackTrace}");
  79. }
  80. }
  81. public async Task DeleteBloodPressReferenceValueAsync(HmBloodPressReferenceValue bpRef)
  82. {
  83. try
  84. {
  85. var hashModel = new
  86. {
  87. Index = $"{bpRef.Age}-{bpRef.Gender}-{bpRef.Hypertension}"
  88. };
  89. var hash = ComputeHash(hashModel);
  90. var cacheKey = CACHE_KEY_BP_REF_VALUE + $"{hash}";
  91. await RedisHelper.DelAsync(cacheKey);
  92. }
  93. catch (Exception ex)
  94. {
  95. _logger.LogError($"{nameof(DeleteBloodPressReferenceValueAsync)}发生异常:{ex.Message}, {ex.StackTrace}");
  96. }
  97. }
  98. private string ComputeHash(object data)
  99. {
  100. lock (_syncLocker)
  101. {
  102. var value = JsonConvert.SerializeObject(data);
  103. value = Convert.ToBase64String(_md5.ComputeHash(Encoding.UTF8.GetBytes(value)));
  104. return value;
  105. }
  106. }
  107. }
  108. }