Files
2023-05-23 10:47:19 +08:00

77 lines
2.6 KiB
C#
Raw Permalink 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 Microsoft.Extensions.Caching.Memory;
namespace OSS.Tools.Cache
{
/// <summary>
/// 缓存过期时间参数
/// </summary>
public class CacheTimeOptions
{
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
/// <summary>
/// 绝对过期时间
/// </summary>
public DateTimeOffset? absolute_expiration { get; set; }
/// <summary>
/// 固定过期时间间隔
/// </summary>
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;
}
}
/// <summary>
/// 滚动过期时长,访问后自动延长,如果同时设置固定过期,则只能在固定时长范围内延长
/// </summary>
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
};
}
}
}