首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net2.0缓存

Posted on 2006-04-06 21:28  小菜  阅读(281)  评论(0)    收藏  举报
OutputCache
<%@ OutputCache Duration="60" VaryByParam="none" %>
Duration 缓存间隔时间,单位秒
VaryByParam 是否通过提交不同参数而缓存页面
DiskCacheable ="true" :硬盘缓存,当数据量比较大的时候使用;例大容量的dataset

dataCaching
Dim Source As DataView

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

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"


sql Caching
<%@ OutputCache Duration="999999" SqlDependency="pubs.authors" VaryByParam="none" %>
监视有限的库和表,注册1,2个较合适
只有当数据库数据改变时,缓存才改变
配置SQL连接池
文件 aspnet_regsql.exe
文件位置\windows\microsoft.net\framework\你的.net framework的版本号
参数
-s指定注册的服务器
-e使用windows授权模式
-d指定数据库名字
-ed缓存生效
aspnet_regsql.exe -s "sqlexpress" -e -d "pubs" -ed 注册库
aspnet_regsql.exe -s "sqlexpress" -e -d "pubs" -et -t "authors"

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>


回调缓存 偏向API和HTML上下文(未测试)
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
.cs/.vb
    Shared Function GetCurrentDate(ByVal context As HttpContext) As String
        
Return Now.ToString()
    
End Function


page Fragment Caching(未测试)
在页面中,通过指定返回参数,决定控件被缓存的部分,使用VaryByParam语句指定控件根据参数改变而改变