using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
namespace RB_MES_API.Context
{
///
/// 缓存处理
///
public class CacheHelper
{
//public CacheHelper Defalut=new CacheHelper();
private static IMemoryCache _cache =new MemoryCache(new MemoryCacheOptions());
///
/// 读取缓存
///
///
///
public static T GetCache(string cacheKey)
{
if(string.IsNullOrEmpty(cacheKey))
{
throw new ArgumentNullException(nameof(cacheKey));
}
T cache;
_cache.TryGetValue(cacheKey, out cache);
return cache;
}
///
/// 设置缓存(滑动过期)
///
///
/// 关键字
/// 缓存值
public static void Set_AbsluteExpire(string key,T value)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
T v;
if(_cache.TryGetValue(key, out v))
{
_cache.Remove(key);
}
_cache.Set(key, value, TimeSpan.FromMinutes(20));
}
///
/// 移除缓存
///
///
public static void Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
_cache.Remove(key);
}
public void Dispose()
{
if(_cache != null) { _cache.Dispose(); }
GC.SuppressFinalize(this);
}
}
}