介紹
1.Duration
該屬性是必須的,屬性值以秒爲單位的正整數,表示頁面從請求開始,持續緩存的事件。2.VaryByParam
在ASP.NET中,一個頁面可以根據傳入參數的不同顯示不同的結果。以商品查詢系統爲例,根據編號不同顯示不同商品信息 ,其URL地址如“detail.aspx?id=1”等每個商品的信息都是
商品詳情頁面被緩存了1200秒,當VaryByParam屬性設置爲“none”,此時頁面輸出緩存忽
所以即使傳入的參數不同,詳情頁面也只緩存一個結果,通過設置VaryByParam屬爲“id”
如果傳遞的參數很多個,可以用分號分隔每個參數
VaryByParam="name;id"
如果希望通過所有參數來緩存頁面 則將屬性設置爲星號“*”
3.VarByControl
如果篩選的數據相對固定,爲了提高性能,則需要使用整頁緩存的技術
當用戶請求訪問頁面時,該頁面將會緩存在服務器內存中,持續時間爲120秒。
在緩存的有效時間內,再次訪問頁面時,即使選擇了其他部門時,顯示仍是要查看的員工
通過設置 OutputCache指令的VaryByControl屬性是DropDownList的id值,即可解決該問題,
代碼示例
<form id="form1" runat="server">
<div>
<a href="T2-1.aspx?id=1">1</a>
<a href="T2-1.aspx?id=2">2</a>
</div>
</form>
//跳轉後臺代碼
namespace ch6
{
public partial class T2_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write("商品" + Request["id"]);
}
}
}
}
<form id="form1" runat="server">
<div>
</div>
</form>
緩存設置
<%@ OutputCache Duration="11111" VaryByParam="id" %>
代碼實例2
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" AutoPostBack="True" OnTextChanged="DropDownList1_TextChanged">
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
<asp:ListItem Value="2">3</asp:ListItem>
<asp:ListItem Value="3">4</asp:ListItem>
<asp:ListItem Value="4">5</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
<%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="DropDownList1" %>