Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

109 Zeilen
3.9KB

  1. using Newtonsoft.Json;
  2. using System.Reflection;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using HealthMonitor.Core.Common;
  6. using HealthMonitor.Util.Common.Cache;
  7. using HealthMonitor.Util.Entities.Base;
  8. using HealthMonitor.Util.Models;
  9. using HealthMonitor.Util.Entities.GpsCard;
  10. namespace HealthMonitor.Core.Cache
  11. {
  12. public class DurableEntityManager : IDurableEntityManager, IDisposable
  13. {
  14. private const string DurableCacheKey = "Entity";
  15. private static object _syncLocker = new object();
  16. private readonly Dictionary<Type, IEntityCacheHandler> _dictDurableEntities;
  17. private MD5? _md5;
  18. public DurableEntityManager()
  19. {
  20. var typesDurable = Assembly.GetAssembly(typeof(EntityBase))?.GetTypes()
  21. .Where(type => !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith("HealthMonitor.Util.Entities"))
  22. .Where(type => type.IsSubclassOf(typeof(EntityBase)) && type.GetCustomAttribute<DurableCacheEntityAttr>() != null)
  23. .ToList();
  24. _dictDurableEntities = new Dictionary<Type, IEntityCacheHandler>();
  25. typesDurable?.ForEach(e =>
  26. {
  27. var attr = e.GetCustomAttribute<DurableCacheEntityAttr>();
  28. _dictDurableEntities.Add(e, new EntityCacheHandler(e, attr!.Duration));
  29. });
  30. _md5 = MD5.Create();
  31. }
  32. public IEntityCacheHandler? GetCacheHandler(Type entityType)
  33. {
  34. return _dictDurableEntities.ContainsKey(entityType) ? _dictDurableEntities[entityType] : null;
  35. }
  36. public string CalcCacheKey(Type entityType, string id)
  37. {
  38. string typeName = entityType.Name;
  39. return $"{DurableCacheKey}_{typeName}#{id}";
  40. }
  41. public string CalcCacheKey(Type entityType, GeneralParam conditions)
  42. {
  43. string typeName = entityType.Name;
  44. string hash = ComputeHash(conditions);
  45. var key = $"{DurableCacheKey}_{typeName}${hash}";
  46. return key;
  47. }
  48. public string CalcHistoryCacheKey(Type entityType, string id, string imei, DateTime? date = null)
  49. {
  50. string typeName = entityType.Name;
  51. var historyDate = date ?? DateTime.Now;
  52. if (entityType == typeof(GpsBloodPressReferenceValue)) id = $"{historyDate.ToString("yyyyMM")}_{DeviceUtils.GetImeiSuffix(imei)}_{id}";
  53. else id = $"{historyDate.ToString("yyyyMM")}_{id}";
  54. return $"{DurableCacheKey}_{typeName}#{id}";
  55. }
  56. public string CalcHistoryCacheKey(Type entityType, GeneralParam conditions, string imei, DateTime? date = null)
  57. {
  58. string typeName = entityType.Name;
  59. var historyDate = date ?? DateTime.Now;
  60. string hash = ComputeHash(conditions);
  61. if (entityType == typeof(GpsBloodPressReferenceValue)) hash = $"{historyDate.ToString("yyyyMM")}_{DeviceUtils.GetImeiSuffix(imei)}_{hash}";
  62. else hash = $"{historyDate.ToString("yyyyMM")}_{hash}";
  63. var key = $"{DurableCacheKey}_{typeName}${hash}";
  64. return key;
  65. }
  66. public void CleanUpEntitiesMapper()
  67. {
  68. foreach (var key in _dictDurableEntities.Keys)
  69. {
  70. _dictDurableEntities[key].CleanUpExpiredMapper();
  71. }
  72. }
  73. private string ComputeHash(object data)
  74. {
  75. lock (_syncLocker)
  76. {
  77. var value = JsonConvert.SerializeObject(data);
  78. //value = BitConverter.ToString(_md5.ComputeHash(Encoding.UTF8.GetBytes(value)));
  79. value = Convert.ToBase64String(_md5!.ComputeHash(Encoding.UTF8.GetBytes(value)));
  80. return value;
  81. }
  82. }
  83. public void Dispose()
  84. {
  85. if (_md5 != null)
  86. {
  87. _md5.Dispose();
  88. _md5 = null;
  89. }
  90. }
  91. }
  92. }