我正在使用.net 4.0 HttpContext.Current.Cache.Add()
将对象插入到我的应用程序的缓存中。在.aspx控制面板页面中,我想显示所有缓存对象及其在插入时指定的相应过期日期。怎么做?
答案 0 :(得分:2)
如果我理解你,你想显示插入的静态截止日期,对吧?如果是这样,您只需存储过期日期并将其传递给控制面板。如果您使用的是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;
}
但是,如果您想要“即时”过期,则必须使用反射。为此,请参考建议对您的问题发表评论的帖子。