Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

asp.net - list OutputCache entry

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...