页面输出缓存应用

一、 页面缓存实现方法:

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申请页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值