C#缓存absoluteExpiration、slidingExpiration两个参数的疑惑
看了很多资料终于搞明白cache中absoluteExpiration,slidingExpiration这两个参数的含义。
absoluteExpiration:用于设置绝对过期时间,它表示只要时间一到就过期,所以类型为System.DateTime,当给这个参数设置了一个时间时,slidingExpiration参数的值就只能为Cache.NoSlidingExpiration,否则出错;
slidingExpiration:用于设置可调过期时间,它表示当离最后访问超过某个时间段后就过期,所以类型为System.TimeSpan,当给这个参数设置了一个时间段时,absoluteExpiration的值就只能为Cache.NoAbsoluteExpiration,否则出错;
两个使用实例
Cache.Add("name", content, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Normal, null);
Cache.Add("name", content, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
//
// 摘要:
// Inserts an object into the System.Web.Caching.Cache object together with dependencies,
// expiration policies, and a delegate that you can use to notify the application
// before the item is removed from the cache.
//
// 参数:
// key:
// The cache key that is used to reference the object.
//
// value:
// The object to insert into the cache.
//
// dependencies:
// The file or cache key dependencies for the item. When any dependency changes,
// the object becomes invalid and is removed from the cache. If there are no dependencies,
// this parameter contains null.
//
// absoluteExpiration:
// The time at which the inserted object expires and is removed from the cache.
// To avoid possible issues with local time such as changes from standard time to
// daylight saving time, use System.DateTime.UtcNow instead of System.DateTime.Now
// for this parameter value. If you are using absolute expiration, the slidingExpiration
// parameter must be set to System.Web.Caching.Cache.NoSlidingExpiration.
//
// slidingExpiration:
// The interval between the time that the cached object was last accessed and the
// time at which that object expires. If this value is the equivalent of 20 minutes,
// the object will expire and be removed from the cache 20 minutes after it was
// last accessed. If you are using sliding expiration, the absoluteExpiration parameter
// must be set to System.Web.Caching.Cache.NoAbsoluteExpiration.
//
// onUpdateCallback:
// A delegate that will be called before the object is removed from the cache. You
// can use this to update the cached item and ensure that it is not removed from
// the cache.
//
// 异常:
// T:System.ArgumentNullException:
// The key, value, or onUpdateCallback parameter is null.
//
// T:System.ArgumentOutOfRangeException:
// You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent
// of more than one year.
//
// T:System.ArgumentException:
// The absoluteExpiration and slidingExpiration parameters are both set for the
// item you are trying to add to the Cache.-or-The dependencies parameter is null,
// and the absoluteExpiration parameter is set to System.Web.Caching.Cache.NoAbsoluteExpiration,
// and the slidingExpiration parameter is set to System.Web.Caching.Cache.NoSlidingExpiration.
public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
namespace HuaTong.General.Utility
{
/// <summary>
/// 缓存操作,默认缓存1分钟
/// </summary>
public
static
class CacheHelper
{
static
int
cacheTime = 1;
/// <summary>
/// 读取缓存项
/// </summary>
/// <
returns
></
returns
>
public
static
object CacheReader(string cacheKey)
{
return
HttpRuntime.Cache[cacheKey];
}
/// <summary>
/// 写入缓存项
/// </summary>
public
static
void CacheWriter(string cacheKey, object cacheValue,
int
cache_time = 0)
{
HttpRuntime.Cache.
Insert
(cacheKey, cacheValue,
null
,
DateTime.Now.AddMinutes(cache_time <= 0 ? cacheTime : cache_time),
Cache.NoSlidingExpiration);
}
/// <summary>
/// 移除指定缓存项
/// </summary>
public
static
void CacheRemove(string cacheName)
{
HttpRuntime.Cache.Remove(cacheName);
}
/// <summary>
/// 缓存对象泛型实现
/// </summary>
public
static
T ObjectReader<T>(string cacheKey =
null
)
where
T : class
{
string cachekey = typeof(T).GetHashCode() + StringHelper.ToString(cacheKey);
var obj = CacheReader(cachekey)
as
T;
return
obj;
}
/// <summary>
/// 缓存对象泛型实现
/// </summary>
public
static
void ObjectWriter<T>(T cacheValue, string cacheKey =
null
,
int
cache_time = 0)
where
T : class
{
string cachekey = typeof (T).GetHashCode() + StringHelper.ToString(cacheKey);
CacheWriter(cachekey, cacheValue, cache_time);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-08-02 从网页监听Android设备的返回键