ASP.NET 2.0 缓存技术

一、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)) %>
          </b>
四、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>
   -->

<以上是个人在网上收集而来!>

posted on 2010-05-21 16:46  safd   阅读(229)  评论(0编辑  收藏  举报

导航