NDB.Extensions.Caching

messaging
Tudor Stanciu 2021-04-25 03:50:14 +03:00
parent 1fa579112f
commit 0006baa27d
6 changed files with 119 additions and 1 deletions

View File

@ -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<ICacheService, CacheService>();
}
}
}

View File

@ -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<T>(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<T> GetAsync<T>(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class
{
var result = await distributedCache.GetStringAsync(key, token);
return result == null ? null : JsonConvert.DeserializeObject<T>(result);
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.14" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View File

@ -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<string, string> _keys = new ConcurrentDictionary<string, string>();
const int expirationTimeFromNow = 24;
public CacheService(IDistributedCache cache)
{
_cache = cache;
}
public async Task<T> GetAsync<T>(string key, CancellationToken token = default(CancellationToken)) where T : class
{
var result = await _cache.GetAsync<T>(key, token);
return result;
}
public async Task SetApplicationDataAsync<T>(string key, T value, CancellationToken token = default(CancellationToken)) where T : class
{
_keys.TryAdd(key, string.Empty);
await _cache.SetAsync<T>(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();
}
}
}
}

View File

@ -0,0 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
namespace NDB.Extensions.Caching.Services
{
public interface ICacheService
{
Task<T> GetAsync<T>(string key, CancellationToken token = default) where T : class;
Task SetApplicationDataAsync<T>(string key, T value, CancellationToken token = default) where T : class;
void Reset();
}
}

12
NDB.sln
View File

@ -50,7 +50,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "security", "security", "{42
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "authentication", "authentication", "{B8132F39-6677-4D70-84CA-9747DC9086B3}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "authentication", "authentication", "{B8132F39-6677-4D70-84CA-9747DC9086B3}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -117,6 +125,8 @@ Global
{420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E} = {E0202271-4E92-4DB8-900D-B5FD745B9278} {420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E} = {E0202271-4E92-4DB8-900D-B5FD745B9278}
{B8132F39-6677-4D70-84CA-9747DC9086B3} = {420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E} {B8132F39-6677-4D70-84CA-9747DC9086B3} = {420A97AE-8E1E-4ECF-AAC5-455ABAA9B17E}
{5C0637C8-6BA4-4EAE-97CA-BB8D98B2991A} = {B8132F39-6677-4D70-84CA-9747DC9086B3} {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 EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87541BAB-3FAC-4ADB-A7FB-8228DA87843D} SolutionGuid = {87541BAB-3FAC-4ADB-A7FB-8228DA87843D}