获取 HttpContext.Current.Cache 的绝对过期



在我的 asp.net 应用程序中,我像这样设置缓存;

 const string key = "MyTestKey";
 object value = true;
 DateTime absoluteExpiration = DateTime.Now.AddMinutes(10);
 TimeSpan slidingExpiration = Cache.NoSlidingExpiration;
 const CacheItemPriority priority = CacheItemPriority.Default;
 CacheItemRemovedCallback onRemoveCallback = null;
 HttpContext.Current.Cache.Add(key, value, null, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);

如何在下一个请求中检索回 HttpContext.Current.Cache 对象的 AbsoluteExpires?或者我可以获取我设置的缓存剩余时间的时间跨度吗?

一旦将项目添加到 System.Web.Caching.Cache,就无法检索该项目的过期时间或其他 CacheItem 信息,除非删除(通过调用 Remove 或缓存过期显式删除)或更新了该项目。存在的唯一方法检索对象仅返回对象。

如果你想要更高级的缓存功能,你需要考虑使用类似MemoryCache的东西。

最新更新