一、 页面缓存实现方法:
1、使用@OutputCache
2、API设置页面缓存功能的内容
通过Response.Cache来获取API对缓存的处理
二、页面缓存的实际例子
1、Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="5" VaryByParam="location" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style ="width:240">
<legend> 设置页面输出缓存</legend>
<center>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</center>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx?location=beijing" >缓存时间</asp:HyperLink>
</fieldset>
</div>
</form>
</body>
</html>
2、Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.Server);//缓存只用于服务器
string temp_location=Request.QueryString["location"];
if (temp_location == null)
{
Response.Cache.SetNoServerCaching();
Label1.Text = "停止缓存时间:" + DateTime.Now.ToString();
}
else
{
Label1.Text = "设置了缓存时间:"+DateTime.Now.ToString();
}
}
3、实例作用说明:
在源码页中利用了@OutputCache指令,并且指定参数为“location”,在cs代码页中设定,缓存只用于服务器,
判断Location是否为空,如果为空则停止缓存,不为空,则使用@OutputCache指令执行缓存功能
4、主要作用:
本例中设置缓存为5秒钟,在这时间内,如果刷新当前页面数据直接可从内存中提出页面,否则就在向IIS申请页面