diff --git a/src/extensions/caching/Netmash.Extensions.Caching/DistributedCachingExtensions.cs b/src/extensions/caching/Netmash.Extensions.Caching/Extensions/DistributedCacheExtensions.cs similarity index 73% rename from src/extensions/caching/Netmash.Extensions.Caching/DistributedCachingExtensions.cs rename to src/extensions/caching/Netmash.Extensions.Caching/Extensions/DistributedCacheExtensions.cs index ce909c0..1d4be25 100644 --- a/src/extensions/caching/Netmash.Extensions.Caching/DistributedCachingExtensions.cs +++ b/src/extensions/caching/Netmash.Extensions.Caching/Extensions/DistributedCacheExtensions.cs @@ -3,14 +3,14 @@ using Newtonsoft.Json; using System.Threading; using System.Threading.Tasks; -namespace Netmash.Extensions.Caching +namespace Netmash.Extensions.Caching.Extensions { - internal static class DistributedCachingExtensions + internal static class IDistributedCacheExtensions { public static async Task SetAsync(this IDistributedCache distributedCache, string key, T value, DistributedCacheEntryOptions options, CancellationToken token = default) where T : class { - var v = JsonConvert.SerializeObject(value); - await distributedCache.SetStringAsync(key, v, options, token); + var valueString = JsonConvert.SerializeObject(value); + await distributedCache.SetStringAsync(key, valueString, options, token); } public static async Task GetAsync(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class diff --git a/src/extensions/caching/Netmash.Extensions.Caching/Services/CacheService.cs b/src/extensions/caching/Netmash.Extensions.Caching/Services/CacheService.cs index 1b9c66c..88d9029 100644 --- a/src/extensions/caching/Netmash.Extensions.Caching/Services/CacheService.cs +++ b/src/extensions/caching/Netmash.Extensions.Caching/Services/CacheService.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.Caching.Distributed; +using Netmash.Extensions.Caching.Extensions; using System; using System.Collections.Concurrent; -using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -10,7 +10,7 @@ namespace Netmash.Extensions.Caching.Services internal class CacheService : ICacheService { private readonly IDistributedCache _cache; - private static readonly ConcurrentDictionary _keys = new ConcurrentDictionary(); + private static readonly ConcurrentBag _keys = new(); const int expirationTimeFromNow = 24; @@ -19,27 +19,24 @@ namespace Netmash.Extensions.Caching.Services _cache = cache; } - public async Task GetAsync(string key, CancellationToken token = default(CancellationToken)) where T : class + public async Task GetAsync(string key, CancellationToken token = default) where T : class { var result = await _cache.GetAsync(key, token); return result; } - public async Task SetApplicationDataAsync(string key, T value, CancellationToken token = default(CancellationToken)) where T : class + public async Task SetAsync(string key, T value, CancellationToken token = default) where T : class { - _keys.TryAdd(key, string.Empty); - await _cache.SetAsync(key, value, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(expirationTimeFromNow) }, token); + _keys.Add(key); + await _cache.SetAsync(key, value, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(expirationTimeFromNow) }, token); } public void Reset() { lock (_keys) { - var list = _keys.Keys.ToList(); - foreach (var s in list) - { - _cache.Remove(s); - } + foreach (var key in _keys) + _cache.Remove(key); _keys.Clear(); } } diff --git a/src/extensions/caching/Netmash.Extensions.Caching/Services/ICacheService.cs b/src/extensions/caching/Netmash.Extensions.Caching/Services/ICacheService.cs index 95e5e33..f5c62b4 100644 --- a/src/extensions/caching/Netmash.Extensions.Caching/Services/ICacheService.cs +++ b/src/extensions/caching/Netmash.Extensions.Caching/Services/ICacheService.cs @@ -6,7 +6,7 @@ namespace Netmash.Extensions.Caching.Services public interface ICacheService { Task GetAsync(string key, CancellationToken token = default) where T : class; - Task SetApplicationDataAsync(string key, T value, CancellationToken token = default) where T : class; + Task SetAsync(string key, T value, CancellationToken token = default) where T : class; void Reset(); } }