简体   繁体   English

查询HttpContext.Current.Cache中的项目

[英]Querying items in HttpContext.Current.Cache

Its simple to add items into this cache: 将项目添加到此缓存很简单:

HttpContext.Current.Cache.Add(
    string key,
    Object value,
    CacheDependency dependencies,
    DateTime absoluteExpiration,
    TimeSpan slidingExpiration,
    CacheItemPriority priority,
    CacheItemRemovedCallback onRemoveCallback
    );

It would be great if I could query the cache and retrieve the expiry date of each item. 如果可以查询缓存并检索每个项目的到期日期,那将是很好的。

Is this possible? 这可能吗? As far as I can see I can only retrieve the key name. 据我所知,我只能检索密钥名称。

Reference: http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add.aspx 参考: http : //msdn.microsoft.com/zh-cn/library/system.web.caching.cache.add.aspx

Yes you can achieve using Reflection. 是的,您可以使用反射实现。

foreach (var t in Cache)
        {
            System.Collections.DictionaryEntry entry = (System.Collections.DictionaryEntry)t;
            object key = entry.Key;
            object obj = Cache.GetType().GetMethod("Get", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(Cache, new object[] { key, 1 });
            PropertyInfo prop = obj.GetType().GetProperty("UtcExpires", BindingFlags.NonPublic | BindingFlags.Instance);
            DateTime expire = (DateTime)prop.GetValue(obj, null);
            Response.Write("<br/>" + key + " : " + expire);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM