12
A
回答
23
我剛剛通過反射器中的System.Web.Caching.Cache。似乎涉及到期日的所有事情都被標記爲內部。我發現公開訪問它的唯一地方是通過Cache.Add和Cache.Insert方法。
所以它看起來像你運氣不好,除非你想經歷反思,除非你真的需要這個日期,否則我不會推薦。
但是,如果你想反正這樣做,那麼這裏是一些代碼,會做的伎倆:
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;
}
-3
緩存本身不會過期。它可以有物品(過期)或根本沒有任何物品。
+0
你還沒明白這個問題。 – Softlion 2016-07-25 08:16:55
+0
@Softlion:如果你有任何問題,你爲什麼不回答這個問題? – shahkalpesh 2016-07-25 11:50:40
一個討厭的方式是添加這個對象的日期時間在不同的緩存對象,然後減去一生與Date.Now()。 – JerryGoyal 2017-02-24 19:51:34