Files
OSS.Tools/Cache/OSS.Tools.Cache/DefaultToolCache.cs
T
2021-10-19 23:40:50 +08:00

70 lines
2.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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
{
/// <summary>
/// 默认缓存实现
/// </summary>
public class DefaultToolCache : IToolCache
{
private static readonly MemoryCache _cache=new MemoryCache(new MemoryCacheOptions());
/// <summary>
/// 添加缓存,如果存在则更新为新值
/// </summary>
/// <typeparam name="T">添加缓存对象类型</typeparam>
/// <param name="key">添加对象的key</param>
/// <param name="obj">值</param>
/// <param name="cacheOpt"></param>
/// <returns>是否添加成功</returns>
public Task<bool> SetAsync<T>(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);
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <typeparam name="T">获取缓存对象类型</typeparam>
/// <param name="key">key</param>
/// <returns>获取指定key对应的值 </returns>
public Task<T> GetAsync<T>(string key)
{
return Task.FromResult(_cache.Get<T>(key));
}
/// <summary>
/// 移除缓存对象
/// </summary>
/// <param name="keys"></param>
/// <returns>是否成功</returns>
public Task<bool> RemoveAsync(params string[] keys)
{
foreach (var key in keys)
{
_cache.Remove(key);
}
return Task.FromResult(true);
}
}
}