对Cache进行浏览并管理。例子
<%@ Page Language="C#" EnableViewState = "true"%>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Configuration" %>
<%@ import Namespace="System.Collections" %>
<%@ import Namespace="System.Collections.Specialized" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
//http://aspalliance.com/aldotnet/examples/cacheviewer.aspx
//http://scottwater.com
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
hlRefresh.NavigateUrl = Request.RawUrl;
BindGrid();
}
}
//绑定到DataGrid
private void BindGrid()
{
//创建arraylist来保存cacheditem信息
ArrayList al = new ArrayList();
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
al.Add(new CachedItem(CacheEnum.Key.ToString(),CacheEnum.Value.GetType().ToString()));
}
litCount.Text = al.Count.ToString();
dgCachedItems.DataSource = al;
dgCachedItems.DataBind();
}
//删除Cache项
protected void Grid_ItemCommand(object sender, DataGridCommandEventArgs e)
{
HyperLink l = (HyperLink)e.Item.FindControl("CacheItemName");
//确保我们找到了该项
if(l != null)
{
Cache.Remove(l.Text);
}
this.BindGrid();
}
protected void Grid_Created(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Literal)e.Item.FindControl("Counter")).Text = (e.Item.ItemIndex + 1).ToString();
}
}
private class CachedItem
{
public CachedItem(){}
public CachedItem(string key, string type)
{
this.CacheKey = key;
this.CacheType = type;
}
private string _cacheKey;
public string CacheKey
{
get {return this._cacheKey;}
set {this._cacheKey = value;}
}
private string _cacheType;
public string CacheType
{
get {return this._cacheType;}
set {this._cacheType = value;}
}
}
//Remove All
void lbRemoveAll_Click(object sender, EventArgs e)
{
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
Cache.Remove(CacheEnum.Key.ToString());
}
BindGrid();
}
</script>