我怎样才能得到一个HttpRuntime.Cache对象的到期日期时间? [英] How can I get the expiry datetime of an HttpRuntime.Cache object?
问题描述
时有可能得到期满的DateTime
的 HttpRuntime.Cache
对象?
Is it possible to get the expiry DateTime
of an HttpRuntime.Cache
object?
如果是这样,这将是最好的方法?
If so, what would be the best approach?
推荐答案
我刚刚通过的System.Web.Caching.Cache进去反射器。这似乎是一个涉及过期日期标记为内部的一切。唯一的地方,我发现公众对它的访问,是通过Cache.Add和Cache.Insert方法。
I just went through the System.Web.Caching.Cache in reflector. It seems like everything that involves the expiry date is marked as internal. The only place i found public access to it, was through the Cache.Add and Cache.Insert methods.
所以它看起来像你的运气了,除非你想通过反思,我不建议,除非你真的真的需要这个日期。
So it looks like you are out of luck, unless you want to go through reflection, which I wouldn't recommend unless you really really need that date.
但是,如果你想反正这样做,那么这里是一些code,它会做的伎俩:
But if you wish to do it anyway, then here is some code that would do the trick:
private DateTime GetCacheUtcExpiryDateTime(string cacheKey)
{
object cacheEntry = Cache.GetType().GetMethod("Get", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(Cache, new object[] { cacheKey, 1 });
PropertyInfo utcExpiresProperty = cacheEntry.GetType().GetProperty("UtcExpires", BindingFlags.NonPublic | BindingFlags.Instance);
DateTime utcExpiresValue = (DateTime)utcExpiresProperty.GetValue(cacheEntry, null);
return utcExpiresValue;
}
这篇关于我怎样才能得到一个HttpRuntime.Cache对象的到期日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!