Cache Configuration,快取組態
Machine.config/Web.config
網頁輸出快取組態
<OutputCache />
影響整個網站所有頁面
<OutputCacheSettings />
只影響套用的頁面
個別網頁(Individual Page) 使用者控制項
快取API組態設定
<Cache />
影響網站全域應用程式
SQL快取相依性組態設定
<SqlCacheDependency />
只影響套用的頁面
<%@ OutputCache CacheProfile="Cache3600Seconds" VaryByParam="None"%>
Cache Removal,快取移除
自動移除
Scavenging,記憶體不足
CacheItemPriority
NotRemovable > High > AboveNormal > Normal(Default) > BelowNormal > Low
Expiration,資料過期
Dependency,相依性改變
1. Key Dependence
一個快取項目相依於其他數個快取項目
直接使用CacheDependency類別
2. File Dependence
相依於外部檔案
3. SQL Dependence
相依於MS SQL Server中資料表的變更
使用SqlCacheDependency類別
4. Aggregate Dependence
彙總相依性,相依於多個項目
使用AggregateCacheDependency類別
5. Custom Dependence
在自己程式碼中建立的相依性設定。
自行繼承CacheDependency類別來實作符合需求的相依性類別
手動移除
Cache.Remove("key")
Page output caching
網頁輸出快取
Full page caching
完整網頁快取
<%@ OutputCache Duration="3600" VaryByParam="None" %>
Response.Cache
1. Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600))
2. Response.Cache.SetCacheability(HttpCacheability.Public) Response.Cache --> Page.Response.Cache --> HttpResponse.Cache --> HttpCachePolicy
Partial page caching
部分網頁快取
Control caching
1. 只快取指定的區塊
2. User Control, *.ascx
1. 於*.ascs加入指示
<%@ OutputCache Duration="3600" VaryByParam="None" %> 2. 於*.ascs.vb加入PartialCachingAttribute類別
<PartialCaching(3600)> _ '<-- 指定使用者控制項快取時間
Partial Class UserControls_Name ...
Post-Cache Substitution
快取後置換
1. 指定的區塊不要快取
2. Substitution
3. AdRotator
2.1 Substitution中必須使用「Shared」共用/靜態方法 3.1 AdRotator內部已實作快取後置換
Application data caching
應用程式資料快取
Key / Value (索引鍵/數值)
Cache("FirstName") = "KingKong"
Cache("LastName") = "Bruce"
1. Response.Cache屬性 --> System.Web.HttpResponse.Cache
2. Cache("")物件 --> System.Web.Caching.Cache類別
Cache.Add()
Public Function Add( _
key As String, _ '快取資料項目索引鍵
value As Object, _ '要加入至快取的項目值
dependencies As CacheDependency, _ '相依性,如無為Nothing
absoluteExpiration As DateTime, _ '絕對期限
slidingExpiration As TimeSpan, _ '滑動期限
priority As CacheItemPriority, _ '優先權
onRemoveCallback As CacheItemRemovedCallback _ '當物件從快取移除時,會呼叫委派(如果有)
) As Object
Cache.Add()方法無多載 1. 絕對/滑動期限兩者只能選擇一個來使用
2. 如使用absoluteExpiration則slidingExpiration參數必須為NoSlidingExpiration
3. 如使用slidingExpiration則absoluteExpiration參數必須為NoAbsoluteExpiration
Cache.Add("UserName", txtUserName.Text, Nothing, _
System.WebCaching.Cache.NoAbsoluteExpiration, _
System.Web.Caching.Cache.NoSlidingExpiration, _
System.Web.Caching.CacheItemPriority.Default, _
Nothing)
Cache.Inser()
1. Cache.Insert(key, value)
2. Cache.Insert(key, value, dependencies )
3. Cache.Insert(Key, value, dependencies, absoluteExpiration, slidingExpiration )
4. Cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback )
Cache.Insert()方法本身為多載型式