list OutputCache entry

喜你入骨 提交于 2019-11-27 02:02:04

问题


in my asp.net mvc application i'm using the OutputCache attribute on different action method. Is possible to view the current entries on the cache related to OutputCache attribute? If i cicle on System.Web.HttpContext.Current.Cache i don't find this type of entry. Thanks in advance F.


回答1:


Output Cache is not publicly-accessible so you will not find it in System.Web.HttpContext.Current.Cache . In ASP.NET 2 it is contained in the CacheInternal's _caches member, which you can guess by the name is a private member of an internal abstract class. It is possible to retrieve it with reflection, though it is not an easy task.

Also if you retrieve it you'll see that it contains all kinds of internal cache entries like configuration files path cache, dynamically generated classes cache, mobile capabilities, raw response cache (this one is the type of the output cache items).

Let's say that you can filter the items related to output cache. The problem is that they don't contain much human readable information apart from the key and raw response (as byte array). The key generally consists of information if GET (a1) or POST (a2) method i used, the site name, root relative url and hash of the dependent parameters.

I guess it was a common pain-point so in ASP.NET 4 a new concept of custom output cache providers was introduced. You can implement your own output cache provider inheriting from OutputCacheProvider and provide a method that returns all entries. You can check out this article - http://weblogs.asp.net/gunnarpeipman/archive/2009/11/19/asp-net-4-0-writing-custom-output-cache-providers.aspx. I personally haven't looked in the new OutputCache infrastructure, so if you find anything it will be interesting to write about it.

This is the code to retrieve the internal cache entries. You can browse their values while debugging in Visual Studio:

Type runtimeType = typeof(HttpRuntime);

PropertyInfo ci = runtimeType.GetProperty(
   "CacheInternal", 
   BindingFlags.NonPublic | BindingFlags.Static);

Object cache = ci.GetValue(ci, new object[0]);

FieldInfo cachesInfo = cache.GetType().GetField(
    "_caches", 
    BindingFlags.NonPublic | BindingFlags.Instance);
object cacheEntries = cachesInfo.GetValue(cache);

List<object> outputCacheEntries = new List<object>();

foreach (Object singleCache in cacheEntries as Array)
{
   FieldInfo singleCacheInfo =
   singleCache.GetType().GetField("_entries",
      BindingFlags.NonPublic | BindingFlags.Instance);
   object entries = singleCacheInfo.GetValue(singleCache);

   foreach (DictionaryEntry cacheEntry in entries as Hashtable)
   {
      FieldInfo cacheEntryInfo = cacheEntry.Value.GetType().GetField("_value",
         BindingFlags.NonPublic | BindingFlags.Instance);
      object value = cacheEntryInfo.GetValue(cacheEntry.Value);
      if (value.GetType().Name == "CachedRawResponse")
      { 
         outputCacheEntries.Add(value);
      }
   }
}


来源:https://stackoverflow.com/questions/3156792/list-outputcache-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!