diff --git a/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheExtensions.cs b/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheExtensions.cs index 654d6139..846ac572 100644 --- a/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheExtensions.cs +++ b/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using Microsoft.Extensions.Caching.Memory; @@ -24,21 +25,19 @@ public static bool Contains(this MemoryCache cache, object key) internal static void RegisterChild(this MemoryCache cache, object parentKey, object childKey) { - object temp; - if (cache.TryGetValue(parentKey, out temp)) + if (cache.TryGetValue(parentKey, out var keys)) { - var set = (HashSet)temp; - set.Add(childKey); + var keySet = (ConcurrentDictionary)keys; + keySet.TryAdd(childKey, true); } } internal static void RemoveChilds(this MemoryCache cache, object region) { - object keys; - if (cache.TryGetValue(region, out keys)) + if (cache.TryGetValue(region, out var keys)) { - var keySet = (HashSet)keys; - foreach (var key in keySet) + var keySet = (ConcurrentDictionary)keys; + foreach (var key in keySet.Keys) { cache.Remove(key); } diff --git a/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheHandle`1.cs b/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheHandle`1.cs index dff819ce..248e4a7d 100644 --- a/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheHandle`1.cs +++ b/src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheHandle`1.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Collections.Concurrent; using CacheManager.Core; using CacheManager.Core.Internal; using CacheManager.Core.Logging; @@ -227,7 +227,7 @@ private void CreateRegionToken(string region) SlidingExpiration = TimeSpan.MaxValue, }; - _cache.Set(region, new HashSet(), options); + _cache.Set(region, new ConcurrentDictionary(), options); } private void ItemRemoved(object key, object value, EvictionReason reason, object state)