diff --git a/NDB.Extensions.Caching/CachingExtensions.cs b/NDB.Extensions.Caching/CachingExtensions.cs new file mode 100644 index 0000000..f200d73 --- /dev/null +++ b/NDB.Extensions.Caching/CachingExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using NDB.Extensions.Caching.Services; + +namespace NDB.Extensions.Caching +{ + public static class CachingExtensions + { + public static void SetCacheProvider(this IServiceCollection services) + { + services.AddDistributedMemoryCache(); + services.AddScoped(); + } + } +} diff --git a/NDB.Extensions.Caching/DistributedCachingExtensions.cs b/NDB.Extensions.Caching/DistributedCachingExtensions.cs new file mode 100644 index 0000000..e04fb94 --- /dev/null +++ b/NDB.Extensions.Caching/DistributedCachingExtensions.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Caching.Distributed; +using Newtonsoft.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace NDB.Extensions.Caching +{ + internal static class DistributedCachingExtensions + { + 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); + } + + public static async Task GetAsync(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class + { + var result = await distributedCache.GetStringAsync(key, token); + return result == null ? null : JsonConvert.DeserializeObject(result); + } + } +} diff --git a/NDB.Extensions.Caching/NDB.Extensions.Caching.csproj b/NDB.Extensions.Caching/NDB.Extensions.Caching.csproj new file mode 100644 index 0000000..2b8b335 --- /dev/null +++ b/NDB.Extensions.Caching/NDB.Extensions.Caching.csproj @@ -0,0 +1,13 @@ + + + + netstandard2.0 + + + + + + + + + diff --git a/NDB.Extensions.Caching/Services/CacheService.cs b/NDB.Extensions.Caching/Services/CacheService.cs new file mode 100644 index 0000000..9c50362 --- /dev/null +++ b/NDB.Extensions.Caching/Services/CacheService.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.Caching.Distributed; +using System; +using System.Collections.Concurrent; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace NDB.Extensions.Caching.Services +{ + internal class CacheService : ICacheService + { + private readonly IDistributedCache _cache; + private static readonly ConcurrentDictionary _keys = new ConcurrentDictionary(); + + const int expirationTimeFromNow = 24; + + public CacheService(IDistributedCache cache) + { + _cache = cache; + } + + public async Task GetAsync(string key, CancellationToken token = default(CancellationToken)) 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 + { + _keys.TryAdd(key, string.Empty); + 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); + } + _keys.Clear(); + } + } + } +} diff --git a/NDB.Extensions.Caching/Services/ICacheService.cs b/NDB.Extensions.Caching/Services/ICacheService.cs new file mode 100644 index 0000000..3a1a98f --- /dev/null +++ b/NDB.Extensions.Caching/Services/ICacheService.cs @@ -0,0 +1,12 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace NDB.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; + void Reset(); + } +} diff --git a/NDB.sln b/NDB.sln index 4d3c9d7..db1e5b3 100644 --- a/NDB.sln +++ b/NDB.sln @@ -50,7 +50,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "security", "security", "{42 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "authentication", "authentication", "{B8132F39-6677-4D70-84CA-9747DC9086B3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Security.Authentication.Identity", "NDB.Security.Authentication.Identity\NDB.Security.Authentication.Identity.csproj", "{5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Security.Authentication.Identity", "NDB.Security.Authentication.Identity\NDB.Security.Authentication.Identity.csproj", "{5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "caching", "caching", "{A206A484-3ACF-4032-8B36-AC93A72B0B88}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Extensions.Caching", "NDB.Extensions.Caching\NDB.Extensions.Caching.csproj", "{3E045EE6-A290-467C-B503-3A6CB0065C97}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -94,6 +98,10 @@ Global {5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}.Release|Any CPU.Build.0 = Release|Any CPU + {3E045EE6-A290-467C-B503-3A6CB0065C97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E045EE6-A290-467C-B503-3A6CB0065C97}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E045EE6-A290-467C-B503-3A6CB0065C97}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E045EE6-A290-467C-B503-3A6CB0065C97}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -117,6 +125,8 @@ Global {420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E} = {E0202271-4E92-4DB8-900D-B5FD745B9278} {B8132F39-6677-4D70-84CA-9747DC9086B3} = {420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E} {5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A} = {B8132F39-6677-4D70-84CA-9747DC9086B3} + {A206A484-3ACF-4032-8B36-AC93A72B0B88} = {B50B55F0-9E6E-4061-9100-E2329A44E76B} + {3E045EE6-A290-467C-B503-3A6CB0065C97} = {A206A484-3ACF-4032-8B36-AC93A72B0B88} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {87541BAB-3FAC-4ADB-A7FB-8228DA87843D}