2013-06-06 35 views
8

是否有任何工具可用于查看HttpRunTime缓存中的缓存数据?
我们有一个将数据缓存到HttpRuntime缓存中的Asp.Net应用程序。给定的默认值是60秒,但后来更改为5分钟。但觉得缓存的数据在5分钟之前刷新。不知道底下发生了什么。

是否有任何工具可用,或者我们如何看到在HttpRunTime Cache ....中缓存的数据以及过期时间...?
以下代码用于将项目添加到缓存。
查看在System.Web.HttpRuntime.Cache中缓存的数据

public static void Add(string pName, object pValue) 
    { 
    int cacheExpiry= int.TryParse(System.Configuration.ConfigurationManager.AppSettings["CacheExpirationInSec"], out cacheExpiry)?cacheExpiry:60; 
    System.Web.HttpRuntime.Cache.Add(pName, pValue, null, DateTime.Now.AddSeconds(cacheExpiry), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null); 
    } 


谢谢。

回答

13

Cache类支持IDictionaryEnumerator枚举缓存中的所有键和值。

IDictionaryEnumerator enumerator = System.Web.HttpRuntime.Cache.GetEnumerator(); 
while (enumerator.MoveNext()) 
{ 
    string key = (string)enumerator.Key; 
    object value = enumerator.Value; 
    ... 
} 

但我不相信有任何官方的方式来访问元数据,如到期时间。

3

Cache类支持IDictionaryEnumerator枚举缓存中的所有键和值。以下代码是如何从缓存中删除每个密钥的示例:

List<string> keys = new List<string>(); 

// retrieve application Cache enumerator 
IDictionaryEnumerator enumerator = System.Web.HttpRuntime.Cache.GetEnumerator(); 

// copy all keys that currently exist in Cache 
while (enumerator.MoveNext()) 
{ 
    keys.Add(enumerator.Key.ToString()); 
} 

// delete every key from cache 
for (int i = 0; i < keys.Count; i++) 
{ 
    HttpRuntime.Cache.Remove(keys[i]); 
}