ASP.NET 2.0 缓存技术

Csdn dotnet blog 专家群

一、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>

  <%@ 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 >
看更多.net技术文章 到Csdndotnet 频道( http://dotnet.csdn.net)。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值