list OutputCache entry
Asked Answered
B

1

11

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.

Barbell answered 1/7, 2010 at 10:7 Comment(0)
M
12

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);
      }
   }
}
Miguelinamiguelita answered 10/12, 2010 at 16:21 Comment(5)
I'll give it a try on monday and tell you results. Thank you.Gate
Hi Branislav, is there a way to filter the cache in order to get only my entries? ThanksGate
Yes, you can find the output cache entries gathered in the outputCacheEntries list. Sadly there is not much to see. Nevertheless I hope it helps your case.Miguelinamiguelita
@BranislavAbadjimarinov Perfect! And how we can get the stored value of VarByCustom in cached items? I want to filter outputCacheEntries by VarByCustom value.Scapolite
I mean I want to get the returned value of GetVaryByCustomString function, produced for every cached items.Scapolite

© 2022 - 2024 — McMap. All rights reserved.