Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DurableEntityManager.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }