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