C# Cache 设定过期时间的方法
1. 设定绝对过期时间
/// <summary> /// 设定绝对的过期时间 /// </summary> /// <param name="CacheKey">key</param> /// <param name="objObject">缓存对象</param> /// <param name="seconds">超过多少秒后过期</param> public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero); }
2.设定相对过期时间
/// <summary> /// 设置当前应用程序指定包含相对过期时间Cache值 /// </summary> /// <param name="CacheKey">key</param> /// <param name="objObject">缓存对象</param> /// <param name="timeSpan">超过多少时间不调用就失效,单位是秒</param> public static void SetCacheTimeSpan(string CacheKey, object objObject,long timeSpan) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, TimeSpan.FromSeconds(timeSpan)); }