#region Copyright (C) 2016 Kevin (OSS开源系列) 公众号:OSSCore
/***************************************************************************
* 文件功能描述:全局插件 - 缓存插件默认实现
*
* 创建人: Kevin
* 创建人Email:1985088337@qq.com
*
*
*****************************************************************************/
#endregion
using Microsoft.Extensions.Caching.Memory;
namespace OSS.Tools.Cache
{
///
/// 默认缓存实现
///
public class DefaultToolCache : IToolCache
{
private static readonly MemoryCache _cache=new(new MemoryCacheOptions());
///
/// 添加缓存,如果存在则更新为新值
///
/// 添加缓存对象类型
/// 添加对象的key
/// 值
///
/// 是否添加成功
public Task SetAsync(string key, T obj, CacheTimeOptions cacheOpt)
{
if (!cacheOpt.sliding_expiration.HasValue && !cacheOpt.absolute_expiration.HasValue&&!cacheOpt.absolute_expiration_relative_to_now.HasValue)
throw new ArgumentNullException(nameof(cacheOpt), "缓存过期时间不正确,需要设置固定过期时间或者相对过期时间");
_cache.Set(key, obj, cacheOpt.ToMemCacheTimeOpt());
return Task.FromResult(true);
}
///
/// 获取缓存对象
///
/// 获取缓存对象类型
/// key
/// 获取指定key对应的值
public Task GetAsync(string key)
{
return Task.FromResult(_cache.Get(key));
}
///
/// 移除缓存对象
///
///
/// 是否成功
public Task RemoveAsync(params string[] keys)
{
foreach (var key in keys)
{
_cache.Remove(key);
}
return Task.FromResult(true);
}
}
}