1. 设定绝对过期时间

1 /// <summary> 2 /// 设定绝对的过期时间 3 /// </summary> 4 /// <param name="CacheKey"></param> 5 /// <param name="objObject"></param> 6 /// <param name="seconds">超过多少秒后过期</param> 7 public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds) 8 { 9 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 10 objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero); 11 }
2.设定相对过期时间

1 /// <summary> 2 /// 设置当前应用程序指定包含相对过期时间Cache值 3 /// </summary> 4 /// <param name="CacheKey"></param> 5 /// <param name="objObject"></param> 6 /// <param name="timeSpan">超过多少时间不调用就失效,单位是秒</param> 7 public static void SetCacheTimeSpan(string CacheKey, object objObject,long timeSpan) 8 { 9 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 10 objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, TimeSpan.FromSeconds(timeSpan)); 11 }