查看System.Web.HttpRuntime.Cache中缓存的数据

Sun*_*nil 12 c# asp.net httpruntime.cache

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

有没有可用的工具或我们如何看到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);
    }
Run Code Online (Sandbox Code Playgroud)


谢谢.

Joe*_*Joe 18

缓存类支持的IDictionaryEnumerator所有键和值枚举在缓存中.

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

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


Pra*_*n04 7

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]);
}
Run Code Online (Sandbox Code Playgroud)