CacheInspector.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

namespace VwdCms.Admin
{
    public class CacheInspector : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Table tblItems;
        protected System.Web.UI.WebControls.Button btnClearAppCache;

        protected void Page_Load(object sender, EventArgs e)
        {
            VwdCms.Configuration.Utilities.ProtectAdminPages();

            this.btnClearAppCache.Command += new CommandEventHandler(btnClearAppCache_Command);

            DisplayAppCache();
        }

        private void DisplayAppCache()
        {
            this.tblItems.Rows.Clear();
            this.tblItems.Controls.Clear();

            TableRow tr = null;
            TableCell tc = null;
            IDictionaryEnumerator cacheEnum = Cache.GetEnumerator();
            object cacheditem = null;
            HyperLink lnk = null;

            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Cache Key";
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = "Cached Object Type";
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = "Options";
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            this.tblItems.Rows.Add(tr);

            string key = null;

            while (cacheEnum.MoveNext())
            {
                key = cacheEnum.Key.ToString();
                cacheditem = cacheEnum.Value;

                if (cacheditem.GetType() == typeof(System.Drawing.Bitmap))
                {
                    lnk = new HyperLink();
                    lnk.Target = "_blank";
                    lnk.Text = "View in Browser";
                    lnk.NavigateUrl = VwdCms.WebResourceCode.GetUrl(key, "image/jpeg", "cache", true);
                }
                else
                {
                    lnk = null;
                }

                tr = new TableRow();

                tc = new TableCell();
                tc.Text = key;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = cacheditem.GetType().FullName;
                tr.Cells.Add(tc);


                tc = new TableCell();
                if (lnk == null)
                {
                    tc.Text = " ";
                }
                else
                {
                    tc.Controls.Add(lnk);
                }
                tr.Cells.Add(tc);

                this.tblItems.Rows.Add(tr);
            }

            HttpCachePolicy hcp = this.Page.Response.Cache;

            

        }
        void btnClearAppCache_Command(object sender, CommandEventArgs e)
        {
            string[] keys = new string[Cache.Count];
            int k = 0;
            IDictionaryEnumerator cacheEnum = Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                keys[k] = cacheEnum.Key.ToString();
                k++;
            }

            foreach (string key in keys)
            {
                Cache.Remove(key);
            }
        }
    }
}