Aw, the HttpContext.Cache. Most of the items I place in the Cache are named with GUID's so it's not very helpful to loop through and print out the Cache Item Name. I've built this handy ASPX page that gives a nice little view of the Cache in human readable form. All the Datasets, are displayed, including the DataTables. now keep in mind, that I'm not worried about the graphic design, or the looks here, I just wanted to make something work. I did add a little javascript to expand/collapse the items since they tend to be rather large.
I'm happy to answer questions, and I'm eager for advice, on a better way, faster way, or just general feedback on the code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="javascript">
<!--
function OpenOrCloseSpan(spanTag)
{
var st = document.getElementById( spanTag );
if ( st.style.display == 'none' )
st.style.display = '';
else
st.style.display = 'none';
}
// -->
</script>
<HTML>
<HEAD>
<title>Display Cache Keys And Values</title>
</HEAD>
<body>
<a href="DisplayCacheKeyValues.aspx">Refresh Page</a>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder ID="phTable" Runat="server" />
</form>
</body>
</HTML>
protected System.Web.UI.WebControls.PlaceHolder phTable;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
GetAndDisplayCacheItems();
}
else
{
CreateAndBindRemoveButtons();
}
}
private void GetAndDisplayCacheItems()
{
StringBuilder sb = new StringBuilder();
HttpContext ctx = HttpContext.Current;
IDictionaryEnumerator d = ctx.Cache.GetEnumerator();
while(d.MoveNext())
{
HtmlTable t = new HtmlTable();
t.BorderColor = "FF0000";
t.CellPadding = 10;
t.CellSpacing = 0;
t.Border = 1;
HtmlTableRow r = new HtmlTableRow();
HtmlTableCell CacheKeyTableCell = new HtmlTableCell();
CacheKeyTableCell.VAlign = "top";
sb.Length = 0;
sb.Append("<B>Cache Item Type: </B>");
sb.Append(ctx.Cache[d.Key.ToString()].GetType().ToString());
sb.Append("<BR /><B>Value: </B><BR />");
sb.Append(Server.HtmlEncode(d.Value.ToString()));
CacheKeyTableCell.Controls.Add(new LiteralControl(sb.ToString()));
r.Cells.Add(CacheKeyTableCell);
HtmlTableCell CacheValueTableCell = new HtmlTableCell();
switch(d.Value.ToString())
{
case "System.Data.DataSet" :
int tblCounter = 0;
DataSet ds = new DataSet();
ds = ((DataSet)ctx.Cache[d.Key.ToString()]).Copy();
foreach(DataTable dt in ds.Tables)
{
bool rowsProcessed = false;
foreach(DataRow dr in dt.Rows )
{
rowsProcessed = true;
for(int x = 0; x < dr.ItemArray.GetLength(0); x++)
{
try
{
string columnData = Server.HtmlEncode(dr[x].ToString());
dr[x] = columnData;
}
catch {}
}
}
if(rowsProcessed)
{
if(tblCounter > 0)
{
CacheValueTableCell.Controls.Add(new LiteralControl("<HR>"));
}
DataGrid dg = new DataGrid();
dg.GridLines = GridLines.Both;
dg.AutoGenerateColumns = true;
dg.BorderColor = Color.Black;
dg.BorderWidth = Unit.Pixel(2);
dg.DataSource = dt;
dg.DataBind();
foreach(DataGridColumn c in dg.Columns)
{
c.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
c.ItemStyle.VerticalAlign = VerticalAlign.Top;
}
sb.Length = 0;
sb.Append("DataTable ");
sb.Append(tblCounter.ToString());
sb.Append("<BR />");
CacheValueTableCell.Controls.Add(new LiteralControl(sb.ToString()));
CacheValueTableCell.Controls.Add(dg);
}
tblCounter++;
}
r.Cells.Add(CacheValueTableCell);
break;
default :
break;
}
t.Rows.Add(r);
phTable.Controls.Add(new LiteralControl("<P>"));
phTable.Controls.Add(CreateRemoveLinkButton(d.Key.ToString()));
phTable.Controls.Add(new LiteralControl(" "));
HtmlAnchor a = new HtmlAnchor();
a.HRef= "Javascript:OpenOrCloseSpan('Span_" + d.Key.ToString() + "')";
a.Controls.Add(new LiteralControl("Open/Close"));
phTable.Controls.Add(a);
StringBuilder CacheItemDisplayName = new StringBuilder();
CacheItemDisplayName.Append(" ");
CacheItemDisplayName.Append(d.Key.ToString());
CacheItemDisplayName.Append("<span id=\"span_");
CacheItemDisplayName.Append(d.Key.ToString());
CacheItemDisplayName.Append("\" style=\"Display: None\">");
phTable.Controls.Add(new LiteralControl(CacheItemDisplayName.ToString()));
phTable.Controls.Add(t);
phTable.Controls.Add(new LiteralControl("</span></P>"));
}
}
private LinkButton CreateRemoveLinkButton(string buttonID)
{
LinkButton RemoveLinkButton = new LinkButton();
RemoveLinkButton.Text = "Remove";
RemoveLinkButton.ID = buttonID;
RemoveLinkButton.Click += new EventHandler(RemoveLinkButton_Click);
return RemoveLinkButton;
}
private void CreateAndBindRemoveButtons()
{
HttpContext ctx = HttpContext.Current;
IDictionaryEnumerator d = ctx.Cache.GetEnumerator();
while(d.MoveNext())
{
phTable.Controls.Add(CreateRemoveLinkButton(d.Key.ToString()));
}
}
private void RemoveLinkButton_Click(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
if(ctx.Cache[((LinkButton)sender).ID] != null)
{
ctx.Cache.Remove(((LinkButton)sender).ID);
}
phTable.Controls.Clear();
GetAndDisplayCacheItems();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion