fix metadata cache cast
ee3915f3
Alexander Nosenko
committed
succeeded
3 changed files
LoggerExtensions.cs
/src/Refactorius.Data.Xrm/Logging/LoggerExtensions.cs
/src/Refactorius.Data.Xrm/Logging/LoggerExtensions.cs
PersistentOrganizationMetadataCache.cs
/src/Refactorius.Data.Xrm/Metadata/PersistentOrganizationMetadataCache.cs-5+19
/src/Refactorius.Data.Xrm/Metadata/PersistentOrganizationMetadataCache.cs
Add comment 4
Add comment 5 internal class PersistentOrganizationMetadataCache
Add comment 6 {
Add comment 7 Plus   // to prevent retrieving the data cached by a previously loaded version of a plugin
Add comment 8 Plus   // (Dataverse doesn't unload previous plugin version immediately))
Add comment 9 Plus   private static string _loadId = Guid.NewGuid().ToString("N");
Add comment 10 Plus  
Add comment 7 11 private readonly MemoryCache _memoryCache = MemoryCache.Default;
Add comment 8 12
Add comment 9 13 private readonly object _lock = new();
Add comment 12 16 public static PersistentOrganizationMetadataCache Default => new();
Add comment 13 17
Add comment 14 18 private static string MakeCacheKey(Guid organizationId)
Add comment 15 Minus   => $"OrganizationMetadata-{organizationId} {typeof(OrganizationMetadata).Assembly.FullName}";
Add comment 19 Plus   => $"OrganizationMetadata-{_loadId}-{organizationId} {typeof(OrganizationMetadata).Assembly.FullName}";
Add comment 16 20
Add comment 17 21 public PersistentOrganizationMetadata AddOrGetExisting(PersistentOrganizationMetadata data)
Add comment 18 22 {
Add comment 19 23 var key = MakeCacheKey(data.OrganizationId);
Add comment 20 24 lock (_lock)
Add comment 21 25 {
Add comment 22 Minus   if (!_memoryCache.Contains(key))
Add comment 26 Plus   if (_memoryCache.Contains(key))
Add comment 23 27 {
Add comment 28 Plus   var obj = _memoryCache[key];
Add comment 29 Plus   if (obj is PersistentOrganizationMetadata existing)
Add comment 30 Plus   return existing;
Add comment 31 Plus  
Add comment 32 Plus   _memoryCache.Remove(key);
Add comment 33 Plus   }
Add comment 34 Plus  
Add comment 24 35 _memoryCache.AddOrGetExisting(
Add comment 25 36 key,
Add comment 26 37 data,
Add comment 27 38 new DateTimeOffset(DateTime.Now.Add(_expirationInterval)));
Add comment 28 Minus   }
Add comment 29 Minus  
Add comment 30 39 return (PersistentOrganizationMetadata)_memoryCache[key];
Add comment 31 40 }
Add comment 32 41 }
Add comment 54 63 {
Add comment 55 64 var key = MakeCacheKey(organizationId.Value);
Add comment 56 65 if (_memoryCache.Contains(key))
Add comment 57 Minus   return (PersistentOrganizationMetadata)_memoryCache[key];
Add comment 66 Plus   {
Add comment 67 Plus   var obj = _memoryCache[key];
Add comment 68 Plus   if (obj is PersistentOrganizationMetadata data)
Add comment 69 Plus   return data;
Add comment 70 Plus   _memoryCache.Remove(key);
Add comment 71 Plus   }
Add comment 58 72 }
Add comment 59 73
Add comment 60 74 return CreateOrGetExisting(organizationService, logger);
Refactorius.Data.Xrm.csproj
/src/Refactorius.Data.Xrm/Refactorius.Data.Xrm.csproj
/src/Refactorius.Data.Xrm/Refactorius.Data.Xrm.csproj