2012-04-11 60 views
0

是否可以獲取特定緩存的到期時間?我希望能找到這樣的事情:獲取緩存的到期時間

HttpRuntime.Cache(key).ExpireTime 
+0

檢查此答案http://stackoverflow.com/a/350374/309973 – Saad 2012-04-11 05:56:36

回答

2

只是爲了方便起見,如果有人打這個帖子,這個作品非常好:

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; 
    } 

原來的答覆這裏找到How can I get the expiry datetime of an HttpRuntime.Cache object?

提個醒,這使用反射。儘管工作得很好,並且以DD/MM/YYYY HH:MM:SS格式爲您提供了一個不錯的UTC日期。