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

Discussion in 'ASP .Net' started by Edward Wilde, Dec 9, 2004.

  1. Edward Wilde

    Edward Wilde Guest

    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.
     
    Edward Wilde, Dec 9, 2004
    #1
    1. Advertising

  2. Edward Wilde

    Edward Wilde Guest

    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.
    "Edward Wilde" <> wrote in message
    news:%...
    > 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.
    >
     
    Edward Wilde, Dec 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .NET Follower
    Replies:
    1
    Views:
    473
    Andy Mortimer [MS]
    Feb 13, 2004
  2. Anders

    Examine the ASP.NET worker process

    Anders, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    Jim Cheshire
    Jan 10, 2006
  3. Pete Griffiths
    Replies:
    0
    Views:
    3,124
    Pete Griffiths
    Feb 21, 2006
  4. Amil Hanish
    Replies:
    1
    Views:
    313
    Juan T. Llibre
    Aug 25, 2006
  5. =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    RE: OutputCache - DropDownList losing items when using varybycontrol

    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=, Nov 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    801
    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
    Nov 27, 2006
Loading...

Share This Page