现在的位置: 首页 > 综合 > 正文

asp.net 缓存使用

2013年09月08日 ⁄ 综合 ⁄ 共 1395字 ⁄ 字号 评论关闭

http://msdn.microsoft.com/zh-cn/library/sfw2210t%28v=vs.80%29

参考微软写了个例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="test4_Default6" %>
<%@ OutputCache CacheProfile="AppCache1" VaryByParam="Color" %>

<!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>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="Color" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Change Color" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

VaryByParam="Color"  缓存局部功能,并没有把整个页面缓存

如果想 整个页面缓存  code 修改 VaryByParam="none"

对应Textbox 控件编号 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test4_Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.BackColor =System.Drawing.Color.FromName(Server.HtmlEncode(Color.Text));
    }
}

webconfig配置 缓存 60秒

    <!-- caching section group -->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="AppCache1" enabled="true" duration="60"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

效果图

1 分钟前效果

1分钟后效果,在一分钟 之间 时间是不变的


抱歉!评论已关闭.