C#从HttpContext.Current.Cache检索缓存过期日期
我正在使用.net 4.0C#从HttpContext.Current.Cache检索缓存过期日期,c#,caching,httpcontext.cache,C#,Caching,Httpcontext.cache,我正在使用.net 4.0HttpContext.Current.Cache.Add()将对象插入到应用程序的缓存中。在.aspx控制面板页面中,我想显示所有缓存对象及其各自的过期日期,这些对象是我在插入时指定的。怎么做?如果我正确理解您的意思,您希望显示插入的静态过期日期,对吗?如果是这样的话,您只需存储过期日期并将其传递给您的控制面板。如果您使用的是asp.net mvc,则可以将此日期作为ViewModel的属性发送。让我们举一个我所说的例子: public DateTime Insert
HttpContext.Current.Cache.Add()
将对象插入到应用程序的缓存中。在.aspx控制面板页面中,我想显示所有缓存对象及其各自的过期日期,这些对象是我在插入时指定的。怎么做?如果我正确理解您的意思,您希望显示插入的静态过期日期,对吗?如果是这样的话,您只需存储过期日期并将其传递给您的控制面板。如果您使用的是asp.net mvc,则可以将此日期作为ViewModel的属性发送。让我们举一个我所说的例子:
public DateTime InsertItemOnCache(object item, DateTime expiration)
{
DateTime dateExpiration;
//Here you construct your cache key.
//You can use your asp.net sessionID if you want to your cache
//to be for a single user.
var key = string.Format("{0}--{1}", "Test", "NewKey");
if (expiration != null)
{
dateExpiration = expiration;
}
else
{
//Set your default time
dateExpiration = DateTime.Now.AddHours(4);
}
//I recommend using Insert over Add, since add will return null if there are
//2 objects with the same key
HttpContext.Current.Cache.Insert(key, item, null, dateExpiration, Cache.NoSlidingExpiration);
return dateExpiration;
}
然而,如果您希望您的到期日期“即时”,则必须使用反射。有关这一点,请参阅建议作为对您问题的评论的帖子。请参阅