Examine items in the ASP.NET cache added using the OutputCache directive


E

Edward Wilde

Hi,

Does anyone know how to examine items in the ASP.NET cache that are added
using the OutputCache directive in framework 1.1

i.e.

<%@ OutputCache Duration="300" VaryByParam="None"
VaryByCustom="cmscontrol,cmsrole"%>

By examine I mean see their value, key and expiration time.

Cheers Ed.
 
Ad

Advertisements

E

Edward Wilde

In the end I cludged it using a bit of hacked together reflection:

<%@ Page language="c#" AutoEventWireup="false" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web.Caching" %>
<script runat=server>
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
base.OnInit(e);
}

private const string row = "<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>";
private void Page_Load(object sender, System.EventArgs e)
{
StringBuilder outHtml = new StringBuilder(512);
outHtml.Append("<table cellpadding=0 cellspacing=0 border=1>");
outHtml.Append("<tr><td>Key</td><td>Value</td><td>Type</td></tr>");

// Grap the CacheInternal cache object from Cache using reflection
Type cacheType = Cache.GetType();
FieldInfo fieldCacheInternal = cacheType.GetField("_cacheInternal",
BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);
Object cacheInternal = fieldCacheInternal.GetValue(Cache);
Hashtable _entries = (Hashtable)
cacheInternal.GetType().GetField("_entries", BindingFlags.DeclaredOnly |
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cacheInternal);

outHtml.Append("<tr><td colspan=3>Count:" + _entries.Count + "</td>");
foreach (DictionaryEntry entry in _entries)
{
Type typeCacheEntry = entry.Value.GetType();
FieldInfo fldKey = typeCacheEntry.BaseType.GetField("_key",
BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo fldValue = typeCacheEntry.GetField("_value",
BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);

outHtml.Append(string.Format(row,
fldKey.GetValue(entry.Value).ToString(),
fldValue.GetValue(entry.Value).ToString(),
fldValue.GetValue(entry.Value).GetType().ToString()));
}

literalOut.Text = outHtml.ToString();
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Cache</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Literal id="literalOut" runat="server"></asp:Literal>
</form>
</body>
</HTML>

Ed.
 

Top