zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SpringBoot Jpa 双数据源mysql + oracle + liquibase+参考源码
    C#:将字符串中连续空格作为分隔符获取多段模糊查询的字符串
    C# 传入参数2021-05-18T00:00:00.000Z使用ToDateTime日期在此基础上加8小时
    修改DbContext并不是线程安全的bug处理。
    产品经理推荐书籍
    抽象类、类和接口
    git 分支合并主干出现冲突的解决办法
    HttpClient请求设置Content-Type标头空格问题
    C# 3Des加密解密
    WPF 颜色选择器
  • 原文地址:https://www.cnblogs.com/emanlee/p/1659450.html
Copyright © 2011-2022 走看看