62,046
社区成员
发帖
与我相关
我的任务
分享
string Key = "testKey";
string FilePath = "test.txt";
protected void Page_Load(object sender, EventArgs e)
{
FilePath = Server.MapPath(FilePath);
}
//删除Cache文件
protected void Button1_Click(object sender, EventArgs e)
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
ViewResult();
}
//删除Cache
protected void Button3_Click(object sender, EventArgs e)
{
if (HttpContext.Current.Cache[Key] != null)
HttpContext.Current.Cache.Remove(Key);
ViewResult();
}
//查看情况
protected void Button2_Click(object sender, EventArgs e)
{
ViewResult();
}
private void ViewResult()
{
if (File.Exists(FilePath))
{
this.Label1.Text = "File存在<br/>";
}
else
{
this.Label1.Text = "File不存在<br/>";
}
if (HttpContext.Current.Cache[Key] != null)
{
this.Label1.Text += "Cache存在<br/>值是" + HttpContext.Current.Cache[Key].ToString();
}
else
{
this.Label1.Text += "Cache不存在<br/>";
}
}
//创建关联
protected void Button4_Click(object sender, EventArgs e)
{
if (HttpContext.Current.Cache[Key] != null)
{
HttpContext.Current.Cache.Remove(Key);
}
if (!File.Exists(FilePath))
{
StreamWriter sw=File.CreateText(FilePath);
sw.WriteLine("测试测试");
sw.Close();
sw.Dispose();
}
CacheDependency cd = new CacheDependency(FilePath);
HttpContext.Current.Cache.Insert(Key, "aaa", cd);
ViewResult();
}