zoukankan      html  css  js  c++  java
  • 超级简单:ASP.NET输出缓存

        ASP.NET缓存通过在内存中存储页面输出来构建一个高性能的,可伸缩的asp.net web应用程序。在随后的请求中,网页代码不用执行,直接使缓存的输出用于服务的请求。在本文中,我们专注于ASP.NET页面输出缓存。

         这只是在一站式开发技术框架中Silverlight样例的一部分,你能从http://cfx.codeplex.com/上获得更多的例子。

        Microsoft All-In-One Code Framework ,微软通过典型的三种流行的编程语言(Visual C#,VB.NET和Visual C++)编写代码示例代码来描绘微软主要的开发技术。每个示例是精心挑选,组成的,并提供测试,或使用的编码方案。

        默认情况下,我们去请求一个ASP.NET 站点,每次请求都将进行多步处理的。 例如页面的初始化,加载,渲染等等。这样会消耗大量的服务器资源。考虑以下情形:许多客户浏览ASP.NET的新闻网页和新闻网站的页面几个小时都不会改变 。基于共同的路线,多个客户的几乎在同一时间在请求同一个新闻网页,ASP.NET将执行相同的代码生成多次相同新闻。这是一种浪费资源的过程。因此,我们开始考虑能否生成一次响应,它能服务于多个客户。答案就是缓存。

        ASP.NET提供了两个缓存的方法。输出缓存(包括页面级缓存和用户控件级缓存)和缓存API 。在本文中,我们将讨论输出缓存。输出缓存的优点是易于实现,并在大多数情况下是足够的。它只是在内存中保存了发送给客户端输出和后续请求答辩书的副本,然后用缓存的输出,直到缓存过期的反应,然后,直到缓存过期,后续的请求响应缓存输出。这令人难以置信的提高ASP.NET Web应用程序的性能。
        对于ASP.NET输出缓存,ASP.NET使用@ OutputCache指令,宣布许多属性来控制ASP.NET页或页面上的用户控件的 输出缓存的策略。
    代码
    <%@ OutputCache Duration="#ofseconds"
       Location
    ="Any | Client | Downstream | Server | None | ServerAndClient "
       Shared
    ="True | False"
       VaryByControl
    ="controlname"
       VaryByCustom
    ="browser | customstring"
       VaryByHeader
    ="headers"
       VaryByParam
    ="parametername" 
       VaryByContentEncoding
    ="encodings"
       CacheProfile
    ="cache profile name | ''"
       NoStore
    ="true | false"
       SqlDependency
    ="database/table name pair | CommandNotification"
    %>

         在本文中,我们将介绍@ OutputCache的 Duration, VaryByCustom, VaryByParam, 和VaryByControl属性来缓存我们的页面输出 ,对于其它属性,您可以参考:http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx.

    下面演练一步一步如何使用它们。在演示中,我们使用的日期时间,以确定页面是否被缓存。(在页面加载事件中,我们将日期时间写在一个名字为“lblResult”的控件中)

    1、Duration属性:

    在ASPX标记中添加@OutputCache指令,并指定到期时间。在这种情况下,我们分配给它10秒。例如: OutputCache Duration="10"  VaryByParam="none"。

    运行ASP.NET Web应用程序,推出这个页面。在页面重新加载时候,我们将看到在网页上的日期时间10秒内不会改变。

    代码
    <%@ OutputCache Duration="10" VaryByParam="none" %>
    <!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>OutPutCacheWithDuration</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="lblResult" runat="server"></asp:Label>
            
    <br />
            
    <br />
            
    <asp:Button ID="btnPostBack" runat="server" Text="Post Back"  />
            
    <p>
                The page will be cached 10s, and then you can 
                       click Button to update datetime.
            
    </p>
        
    </div>
        
    </form>
    </body>
    </html>

     2、VaryByControl属性

    拖放一个DropDownList到页面中和给它增加3个item。

    在ASPX标记中添加@ OutputCache指令,并指定到期时间和VaryByControl属性。 例如: OutputCache Duration="1000" VaryByControl="ddlOption".

    运行ASP.NET Web应用程序和启动这个页面,我们可以看到,不同item有其相应的缓存。

    代码
    <%@ OutputCache Duration="1000" VaryByControl="ddlOption" %>
    <!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>OutPutCacheWithVaryByControl</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="lblResult" runat="server"></asp:Label>
            
    <br />
            
    <br />
            
    <asp:DropDownList ID="ddlOption" runat="server" AutoPostBack="True" 
                       OnSelectedIndexChanged
    ="ddlOption_SelectedIndexChanged">
                
    <asp:ListItem Selected="True">Option One</asp:ListItem>
                
    <asp:ListItem>Option Two</asp:ListItem>
                
    <asp:ListItem>Option Three</asp:ListItem>
            
    </asp:DropDownList>
            
    <p>
                The page will be rendered from cache basing 
                on the selected item of DropDownList. 
                The different item has corresponding cache. 
            
    </p>
        
    </div>
        
    </form>
    </body>
    </html>

     3、VaryByCustom属性

    在ASPX标记中添加@ OutputCache指令,并指定到期时间和设置VaryByControl属性为“browser”。For example: OutputCache Duration="1000"VaryByCustom="browser"VaryByParam="none"。

    运行ASP.NET Web应用程序和使用IE和Firefox(不同的浏览器和版本)启动这个页面,我们将看到,对于不同的浏览器,缓存是不同的。

    代码
    <%@ OutputCache Duration="1000" VaryByCustom="browser" VaryByParam="none" %>
    <!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>OutPutCacheWithVaryByCustom</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="lblResult" runat="server"></asp:Label>
            
    <br />
            
    <br />
            
    <asp:Button ID="btnPostBack" runat="server" Text="Post Back" />
            
    <p>
                The page will be rendered from cache basing 
                on the version of browser, such as IE and FireFox.
            
    </p>
        
    </div>
        
    </form>
    </body>
    </html>

      4、VaryByParam属性

    在ASPX标记中添加@ OutputCache指令,并指定到期时间和指定VaryByParam属性为“id”。例如: OutputCache Duration="1000" VaryByParam="id"。

    运行ASP.NET Web应用程序和启动这个网页, 通过使用QueryString “id”。,你可以请求使用不同的值。

    ~/OutputCacheWithParam.aspx?id=1
    ~/OutputCacheWithParam.aspx?id=2 >
    代码
    <%@ OutputCache Duration="1000" VaryByParam="id" %>
    <!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>OutPutCacheWithVaryByParam</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="lblResult" runat="server"></asp:Label>
            
    <p>
                The page will be rendered from cache until 
                the value of QueryString named "id" is
                changed or Duration is expiration.
            
    </p>
        
    </div>
        
    </form>
    </body>
    </html>

     原文:http://www.codeproject.com/KB/web-cache/OutputCache.aspx



    (全文完)


    以下为广告部分

    您部署的HTTPS网站安全吗?

    如果您想看下您的网站HTTPS部署的是否安全,花1分钟时间来 myssl.com 检测以下吧。让您的HTTPS网站变得更安全!

    SSL检测评估

    快速了解HTTPS网站安全情况。

    安全评级(A+、A、A-...)、行业合规检测、证书信息查看、证书链信息以及补完、服务器套件信息、证书兼容性检测等。

    SSL证书工具

    安装部署SSL证书变得更方便。

    SSL证书内容查看、SSL证书格式转换、CSR在线生成、SSL私钥加解密、CAA检测等。

    SSL漏洞检测

    让服务器远离SSL证书漏洞侵扰

    TLS ROBOT漏洞检测、心血漏洞检测、FREAK Attack漏洞检测、SSL Poodle漏洞检测、CCS注入漏洞检测。

  • 相关阅读:
    初学python遇到的第一个坑
    返回列表中最长的连续字符串
    输入一个数字,求每一位相加之和
    判断一个数是否为素数
    编写一个函数,它接受一个或多个单词的字符串,并返回相同的字符串,但所有五个或多个字母的单词都颠倒过来
    判断10步能不能回到原点
    完成方法/函数,以便将破折号/下划线分隔的单词转换为驼峰式大小写
    求公共汽车上的人数
    写一个函数,返回不同的计数
    对一个数的每一位数字求平方
  • 原文地址:https://www.cnblogs.com/zhuqil/p/1637155.html
Copyright © 2011-2022 走看看