#region Copyright (C) 2016 Kevin (OSS开源系列) 公众号:OSSCore
/***************************************************************************
* 文件功能描述:全局插件 - 缓存选项
*
* 创建人: Kevin
* 创建人Email:1985088337@qq.com
*
*
*****************************************************************************/
#endregion
using Microsoft.Extensions.Caching.Memory;
namespace OSS.Tools.Cache
{
///
/// 缓存过期时间参数
///
public class CacheTimeOptions
{
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
///
/// 绝对过期时间
///
public DateTimeOffset? absolute_expiration { get; set; }
///
/// 固定过期时间间隔
///
public TimeSpan? absolute_expiration_relative_to_now
{
get => this._absoluteExpirationRelativeToNow;
set
{
var zero = TimeSpan.Zero;
if ((value.HasValue ? (value.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
throw new ArgumentOutOfRangeException(nameof(absolute_expiration_relative_to_now), value,
"The relative expiration value must be positive.");
this._absoluteExpirationRelativeToNow = value;
}
}
///
/// 滚动过期时长,访问后自动延长,如果同时设置固定过期,则只能在固定时长范围内延长
///
public TimeSpan? sliding_expiration
{
get => _slidingExpiration;
set
{
var zero = TimeSpan.Zero;
if ((value.HasValue ? (value.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
throw new ArgumentOutOfRangeException(nameof(sliding_expiration), value, "The sliding expiration value must be positive.");
_slidingExpiration = value;
}
}
}
internal static class CacheTimeOptionsExtension
{
public static MemoryCacheEntryOptions ToMemCacheTimeOpt(this CacheTimeOptions cOpt)
{
return new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = cOpt.absolute_expiration_relative_to_now,
SlidingExpiration = cOpt.sliding_expiration,
AbsoluteExpiration = cOpt.absolute_expiration
};
}
}
}