Netmash.Extensions.Caching refactoring

master
Tudor Stanciu 2023-04-15 21:09:51 +03:00
parent ce04d2c142
commit f181b6e810
3 changed files with 13 additions and 16 deletions

View File

@ -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<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);
var valueString = JsonConvert.SerializeObject(value);
await distributedCache.SetStringAsync(key, valueString, options, token);
}
public static async Task<T> GetAsync<T>(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class

View File

@ -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<string, string> _keys = new ConcurrentDictionary<string, string>();
private static readonly ConcurrentBag<string> _keys = new();
const int expirationTimeFromNow = 24;
@ -19,27 +19,24 @@ namespace Netmash.Extensions.Caching.Services
_cache = cache;
}
public async Task<T> GetAsync<T>(string key, CancellationToken token = default(CancellationToken)) where T : class
public async Task<T> GetAsync<T>(string key, CancellationToken token = default) 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
public async Task SetAsync<T>(string key, T value, CancellationToken token = default) where T : class
{
_keys.TryAdd(key, string.Empty);
await _cache.SetAsync<T>(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();
}
}

View File

@ -6,7 +6,7 @@ namespace Netmash.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;
Task SetAsync<T>(string key, T value, CancellationToken token = default) where T : class;
void Reset();
}
}