Netmash.Extensions.Caching refactoring
parent
ce04d2c142
commit
f181b6e810
|
@ -3,14 +3,14 @@ using Newtonsoft.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
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
|
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);
|
var valueString = JsonConvert.SerializeObject(value);
|
||||||
await distributedCache.SetStringAsync(key, v, options, token);
|
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
|
public static async Task<T> GetAsync<T>(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class
|
|
@ -1,7 +1,7 @@
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using Netmash.Extensions.Caching.Extensions;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ namespace Netmash.Extensions.Caching.Services
|
||||||
internal class CacheService : ICacheService
|
internal class CacheService : ICacheService
|
||||||
{
|
{
|
||||||
private readonly IDistributedCache _cache;
|
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;
|
const int expirationTimeFromNow = 24;
|
||||||
|
|
||||||
|
@ -19,27 +19,24 @@ namespace Netmash.Extensions.Caching.Services
|
||||||
_cache = cache;
|
_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);
|
var result = await _cache.GetAsync<T>(key, token);
|
||||||
return result;
|
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);
|
_keys.Add(key);
|
||||||
await _cache.SetAsync<T>(key, value, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(expirationTimeFromNow) }, token);
|
await _cache.SetAsync(key, value, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(expirationTimeFromNow) }, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
lock (_keys)
|
lock (_keys)
|
||||||
{
|
{
|
||||||
var list = _keys.Keys.ToList();
|
foreach (var key in _keys)
|
||||||
foreach (var s in list)
|
_cache.Remove(key);
|
||||||
{
|
|
||||||
_cache.Remove(s);
|
|
||||||
}
|
|
||||||
_keys.Clear();
|
_keys.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Netmash.Extensions.Caching.Services
|
||||||
public interface ICacheService
|
public interface ICacheService
|
||||||
{
|
{
|
||||||
Task<T> GetAsync<T>(string key, CancellationToken token = default) where T : class;
|
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();
|
void Reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue