8、使用编程方式设置页面输出缓存过期
可以使用Response.RemoveOutputCacheItem()方法,以编程的方式从缓存中移除一个页面。
MovieList.aspx
<%@ Page Language="C#" %>
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
</div>
<asp:GridView ID="grdMovies" runat="server" AutoGenerateColumns="False" DataSourceID="dsMovies">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:BoundField DataField="DateReleased" HeaderText="DateReleased" SortExpression="DateReleased" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsMovies" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT * FROM [Movies]"></asp:SqlDataSource>
<br />
<br />
<a href="AddMovie.aspx">Add Movie</a>
</form>
</body>
</html>
AddMovie.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void dvMovie_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
HttpResponse.RemoveOutputCacheItem(Page.ResolveUrl("~/MovieList.aspx"));
Response.Redirect("~/MovieList.aspx");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Add Movie
</h1>
<asp:DetailsView ID="dvMovie" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
Height="50px" AutoGenerateInsertButton="true" Width="125px" OnItemInserted="dvMovie_ItemInserted">
<Fields>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
InsertCommand="INSERT INTO Movies(Title, Director, Description) VALUES (@title,@director,@description)"
SelectCommand="SELECT Movies.* FROM Movies">
<InsertParameters>
<asp:Parameter Name="title" />
<asp:Parameter Name="director" />
<asp:Parameter Name="description" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
2011-5-22 10:32 danny
MovieListKeyDependency.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Cache.Insert("Movies", DateTime.Now);
Response.AddCacheItemDependency("Movies");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:BoundField DataField="DateReleased" HeaderText="DateReleased" SortExpression="DateReleased" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<br />
<br />
<a href="AddMovieKeyDependency.aspx">Add Movie</a>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT * FROM [Movies]"></asp:SqlDataSource>
</form>
</body>
</html>
AddMovieKeyDependency.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void dvMovie_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
Cache.Remove("Movies");
Response.Redirect("~/MovieListKeyDependency.aspx");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Add Movie
</h1>
<asp:DetailsView ID="dvMovie" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
Height="50px" AutoGenerateInsertButton="true" Width="125px" OnItemInserted="dvMovie_ItemInserted">
<Fields>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
InsertCommand="INSERT INTO Movies(Title, Director, Description) VALUES (@title,@director,@description)"
SelectCommand="SELECT Movies.* FROM Movies">
<InsertParameters>
<asp:Parameter Name="title" />
<asp:Parameter Name="director" />
<asp:Parameter Name="description" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
2011-5-22 10:53 danny
9、以编程方式操作页面输出缓存
HttpCachePolicy类
属性
VaryByHeaders
VaryByParams
HttpCachePolicy支持以下方法
AddValidationCallback
AppendCacheExtension
SetAllowResponseInBrowserHistory
SetCacheability
SetETag
SetETagFromFileDependencies
SetExpires
SetLastModified
SetLastModifiedFromFileDependencies
SetMaxAge
SetNoServerCaching
SetNoStore
SetNoTransform
SetOmitVaryStar
SetProxyMaxAge
SetRevalidation
SetValidUnitlExpires
SetVaryByCustom
ProgramOutputCache.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetMaxAge(TimeSpan.FromSeconds(15));
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetOmitVaryStar(true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<a href="ProgramOutputCache.aspx">Request this Page!</a>
</div>
</form>
</body>
</html>
2011-5-22 11:10 danny
10、创建页面输出缓存配置
web.config
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="Movies" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\S4_2.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CachelHour" duration="3600" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
OutputCacheProfile.aspx
<%@ Page Language="C#" %>
<%@ OutputCache CacheProfile="CachelHour" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
</div>
</form>
</body>
</html>
2011-5-22 11:16 danny