页面输出缓存
//最简单的一种,下面的Duration和VaryByParam属性是必须的
<%@ OutputCache Duration="10" VaryByParam="none"%>
//只使用.cs文件来设置页面输出缓存
this.lbShowTime.Text = DateTime.Now.ToString();
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
//通过web.config设置页面输出缓存
//web.config中的配置
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="webcastcache" duration="5" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
//页面文件
<%@ OutputCache CacheProfile="webcastcache" %>
VaryByParam:通过HTTP GET 或HTTP POST中的指定的参数的值来更新缓存,具体代码
//HTTP GET参数,页面文件
<%@ OutputCache Duration="60" VaryByParam="city" %>
//.cs文件如下
string queryStr = Request.QueryString["city"];
if (queryStr == null)
{
queryStr = "";
}
switch (queryStr.ToLower())
{
case "shanghai":
Response.Write("welcome shanghai!");
break;
case "beijing":
Response.Write("welcome beijing");
break;
default:
Response.Write("No Params");
break;
}
//HTTP POST方式,页面文件
<%@ OutputCache Duration="60" VaryByParam="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
VaryByControl:根据指定的控件的有效值来更新缓存
//页面文件如下
<%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
页面输出缓存文件依赖:页面缓存可以依赖于一个或多个文件,当这些文件当中至少一个发生改变时,缓存将被释放的同时更新缓存
//aspx
<asp:Label ID="lbShowTime" runat="server"></asp:Label>
//aspx.cs页面
this.lbShowTime.Text = "CacheByFileDency:" + DateTime.Now.ToString();
//为缓存添加文件依赖
string fileDepencyPath = Server.MapPath("TextFile.txt");
Response.AddFileDependency(fileDepencyPath);
//为缓存添加多文件依赖
string fileDepencyPath1 = Server.MapPath("TextFile.txt");
string fileDepencyPath2 = Server.MapPath("XMLFile.xml");
string[] fileDepencies = new string[] { fileDepencyPath1,fileDepencyPath2};
Response.AddFileDependencies(fileDepencies);
//设置页面输出缓存
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
聚合缓存依赖
Cache["CacheItem1"] = "CacheItem1";
Cache.Insert("CacheItem2", "CacheItem2");
string[] depencies = new string[] { "CacheItem2"};
//监视缓存键的更改
Cache.Insert("CacheItem3", "CacheItem3", new System.Web.Caching.CacheDependency(null, depencies));
//监视XML文件的更改
Cache.Insert("CacheItem4", "CacheItem4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
//聚合缓存依赖的实现
//创建监视XML文件的缓存依赖
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
//创建监视缓存键的依赖
string[] keyDepKey2 = new string[] { "CacheItem1" };
System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDepKey2);
//聚合上面两中依赖,只要两者当中一者变化,则缓存自动更新
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert("CacheItem5", "CacheItem5", aggDep);
Substitution的使用:此控件允许在缓存的页面来实现部分不缓存的功能,第一种实现方式
//aspx页面文件
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" />
//aspx.cs代码文件
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDateTime));
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}
第二种实现方式
//aspx页面文件
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" MethodName="GetCurrentDateTime" />
//aspx.cs代码文件如下
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}