Cache OutputCache ASP.NET缓存

<%@ OutputCache Duration="10" VaryByParam="none" %>

Response.Write(DateTime.Now.ToString());

//==========================================================
if (!this.IsPostBack)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
}
TimeMsg.Text = DateTime.Now.ToString("G");//8/29/2007 2:40:07 PM

//==========================================================
<caching>
    <outputCacheSettings>
   <outputCacheProfiles>
     <add name="test" duration="10" varyByParam="None"/>
     <add name="test1" duration="5" varyByParam="None"/>
   </outputCacheProfiles>
    </outputCacheSettings>
  </caching>

<%@ OutputCache CacheProfile="test1" %>

//==========================================================

<%@ OutputCache Duration="60" VaryByParam="city" %>

<tr>
                <td>
                    <a href="VaryByParam.aspx?city=SZ">SuZhou</a></td>
                <td>
                    <a href="VaryByParam.aspx?city=SH">ShangHai</a></td>
                <td>
                    <a href="VaryByParam.aspx?city=KS">KunShan</a></td>               
            </tr>
//==========================================================
//This page uses post cache substitution to insert a dynamic value into a cached page.

<%@ OutputCache Duration="20" VaryByParam = "none" %>

Time:
<%= DateTime.Now.ToString() %>

Real Time:
<% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDate)); %>

public static string GetCurrentDate(HttpContext context)
    {
        return DateTime.Now.ToString();
    }

//==========================================================
//This page uses post cache substitution to insert a dynamic value into a cached page.

<%@ OutputCache Duration="20" VaryByParam="none" %>

Time:
<%= DateTime.Now.ToString() %>

Real Time:
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />

//==========================================================
//Using the Cache key Dependency

string fileDependencyPath = Server.MapPath("TextFile1.txt");
        Cache.Insert("time", DateTime.Now, new CacheDependency(fileDependencyPath));
        Response.AddCacheItemDependency("time");

        // Set additional properties to enable caching.
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetValidUntilExpires(true);

        TimeMsg.Text = DateTime.Now.ToString("G");

//==========================================================

//Using the File Dependency

if (!this.IsPostBack)
        {
            string fileDependencyPath = Server.MapPath("TextFile1.txt");
            Response.AddFileDependency(fileDependencyPath);

            // Set additional properties to enable caching.
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
            Response.Cache.SetCacheability(HttpCacheability.Public);
            Response.Cache.SetValidUntilExpires(true);
        }

        TimeMsg.Text = DateTime.Now.ToString("G");

//==========================================================
//Cache Control

<%@ OutputCache Duration=30 VaryByControl="TextBox1" %>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e)
    {
        this.Response.Write(DateTime.Now.ToString());
    }

//==========================================================
//Cahce WebUserControl
//最终是根据Page设置的过期时间,本例即为10s
Page:
<%@ OutputCache Duration=10 VaryByParam="None" %>

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

WebUserControl.ascx:
<%@ OutputCache Duration=5 VaryByParam="None" %>

Response.Write(DateTime.Now.ToString());

http://www.cnblogs.com/PLAYBOY840616/archive/2007/08/29/874624.html

原文地址:https://www.cnblogs.com/emanlee/p/1659450.html