ASP.NET 2.0 緩衝技術 (原創)

來源:互聯網
上載者:User

一、SQL Cache 通過設定資料庫串連池,只有當資料庫資料被改變的時候,緩衝才會改變:
          註冊串連池 命令:c:\dir aspnet_regsql.exe 專門註冊串連池的工具 在sql資料庫的資料庫改變了,才改變緩衝。
         參數: _s 制定註冊的伺服器、   _e  說明是windows授權模式、 _u和 _p 說明是sql資料庫的授權模式、
          _d 指定資料庫的名字、_ed 說明緩衝生效。
          案例1、   aspnet_regsql.exe _s "\sqlexpress" _e  _d "pubs" _ed  註冊到pubs庫
         案例2. aspnet_regsql.exe _s "\sqlexpress" _e  _d "pubs" _et _1 "authors" 註冊到pubs庫的表
         <%@ outputcache duuation="999999" sqldependency="pubs;authors" varybyparam="none"%>在頁面中加入
        注意:註冊的庫不要太多,主要的經常用的註冊 最好在兩個庫以內,也不需要每個庫中的的表都註冊
二、Output Caching
      1、 <%@ OutputCache Duration="60" VaryByParam="none" %> 頁面的前面加這句就可以
      2、 Duration  緩衝的間隔時間   VaryByParam   request是否有返回參數改變
      3、 根據參數緩衝  <%@ OutputCache Duration="60" VaryByParam="state" %>
             <SelectParameters>
                <asp:QueryStringParameter Name="state" QueryStringField="state" DefaultValue="CA" />
            </SelectParameters>  這個是在dataview 中設定的
      4、緩衝到硬碟 <%@ OutputCache Duration="3600" VaryByParam="name" DiskCacheable="true" %>
            DiskCacheable 是設定緩衝到硬碟
      5、回調緩衝  一個頁面一部分需要緩衝而其他的不需要緩衝,實現動態緩衝

  <%@ OutputCache Duration="60" VaryByParam="none" %>
       <script runat="server">
        Shared Function GetCurrentDate(ByVal context As HttpContext) As String
        Return Now.ToString()
        End Function
    </script>
       <p>
            <b>Real Time:
                <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
            </b>
        </p>

     6、使用者控制項 的參數緩衝 <%@ OutputCache Duration="60" VaryByControl="pickstate" %>
       <SelectParameters>
            <asp:ControlParameter ControlID="pickstate" DefaultValue="CA" Name="state" PropertyName="SelectedValue" />
        </SelectParameters>

三、Fragment Caching
       API緩衝,比較複雜,一般不採用這中方法
       API回調緩衝 <script runat="server">

            Shared Function GetCurrentDate(context As HttpContext) As String

             return DateTime.Now.ToString()
             End Function

         </script>
          <b>   
           Real Time: <% Response.WriteSubstitution(New HttpResponseSubstitutionCallback(AddressOf GetCurrentDate)) %>

四、Data Caching  dataset<script runat="server">
    Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
        Dim Source As DataView

        ' try to retrieve item from cache
        ' if it's not there, add it
        Source = Cache("MyDataSet")

        If Source Is Nothing Then
            Dim MyConnection As SqlConnection
            Dim MyCommand As SqlDataAdapter

            MyConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("pubsConnectionString").ConnectionString)
            MyCommand = New SqlDataAdapter("select * from Authors", MyConnection)

            Dim ds As New DataSet
            MyCommand.Fill(ds, "Authors")

            Source = New DataView(ds.Tables("Authors"))
            Cache("MyDataSet") = Source

            CacheMsg.Text = "Dataset created explicitly"
        Else
            CacheMsg.Text = "Dataset retrieved from cache"
        End If

        MyGrid.DataSource = Source
        MyGrid.DataBind()
    End Sub
  </script>
  這種資料緩衝的效率比較高,可以緩衝很大的資料量,可以同時緩衝到記憶體和硬碟

五、Web.Config   配置<caching>
   <outputCache>
    <diskCache enabled="true" maxSizePerApp="2" />  設定在硬碟中緩衝 單位是兆
   </outputCache>
   <outputCacheSettings>
    <outputCacheProfiles>
     <add name="CacheFor60Seconds" duration="60" />
    </outputCacheProfiles>
   </outputCacheSettings>
   <!--
   <sqlCacheDependency enabled="true" pollTime="1000" >
    <databases>
     <add name="PubsDB" connectionStringName="pubsConnectionString" />
    </databases>
   </sqlCacheDependency>
   -->
  </caching>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.