ASP.NET 2.0學習3--緩存性能
内容介紹
• 頁面 Caching
• 控件 Caching
• 資料 Caching
• SQL資料Cache
• Cache 配置
一、ASP.NET 2.0 Output Caching
當一個網頁頻繁被通路的時候,我們可以通過把整個網頁緩存來提高執行效率。這樣作的優點是,當使用者再次通路這個網頁的時候,被格式化好的HTML會被直接送顯,
1.Output Cache
• Output Cache 是一項非常有效的增強通路性能的技術,由于IIS的一些特性,預設情況下Output Cache是打開的,但是要對某些請求進行緩存,還需要開發者進行定制。
定制Output Caching
• 對于Output Caching的定制,我們有兩種方法,一種是基于底層的API技術,一種是基于高層的@OutputCaching
• 一旦Output Caching被定制過,那麼這個網頁就會在第一次被通路的時候生成cache,直到請求過期為止。
• 通過使用@Output Cache定義一個緩存頁,顯示目前時間,大家可以看到,當這個頁面被緩存之後,重新整理時,目前時間顯示不發生變化。
示例講解
• 在這個頁面中<%@ Output Cache Duration=“60” VaryByParam=“none”%>
• 這段話定義了頁面将要被緩存,并且,其緩存時間為60秒,并在一個頁面被緩存之後,通過定義varybyparam屬性,它不會因為request接受的參數而改變,隻有在60秒之後,頁面自動清除出緩存,此時,第一個通路提供了新的緩存。
2.參數緩存
性能優勢,由參數改變緩存内容
• 有些時候我們需要根據使用者的請求來生成頁面,但是使用者的請求隻有有限的幾種組合,這個時候就可以根據使用者請求來生成幾種緩存頁面,來進行緩存。
• 接受參數示例,根據使用者選擇不同的state顯示不同的資料庫内容
接收參數示例解說
• 關鍵語句<%@ Output Cache Duration=“60” VaryByParam=“state”%>
• 上面顯示一個資料庫查詢示例,并在頁面下端放置一個通路的目前時間顯示,可以清楚的看到,對于每個不同的參數,一分鐘之内的通路會得到同一個時間戳,這表明使用者在這個時間内通路的是同一個cache
3.硬碟Output Cache
• 預設情況下,Output Cache會被緩存到硬碟上。我們可以通過修改diskcacheenable的屬性來設定其是否緩存,還可以通過在web config裡配置緩存檔案的大小
<caching>
<outputCache>
<diskCache enabled="true" maxSizePerApp="2" />
</outputCache>
</caching>
• 使用DiskOutput Cache
<%@ OutputCache Duration="3600" VaryByParam="name" DiskCacheable="true" %>
4.回調緩存
• 通過設定回調緩存機制,可以針對每個請求在頁面中插入動态的部分,以彌補單獨使用靜态緩存的不足。
• 回調緩存示例
• 通過API實作回調緩存
<%@ OutputCache Duration="60" VaryByParam = "none" %>
<script runat="server">
Shared Function GetCurrentDate(context As HttpContext) As String
return DateTime.Now.ToString()
End Function
</script>
<html>
<head id="Head1" runat="server">
<title>Post Cache Substitution</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>This page uses post cache substitution to insert a dynamic value into a cached page.</b>
<br /><br />
Time: <%= DateTime.Now.ToString() %>
<br /><br />
<b>
Real Time: <% Response.WriteSubstitution(New HttpResponseSubstitutionCallback(GetCurrentDate)) %>
</b>
</div>
</div>
</form>
</body>
</html>
• 通過使用Substitution來實作緩存
<%@ OutputCache Duration="60" VaryByParam="none" %>
<script runat="server">
Shared Function GetCurrentDate(ByVal context As HttpContext) As String
Return Now.ToString()
End Function
</script>
<html>
<head id="Head1" runat="server">
<title>Post Cache Substitution</title>
</head>
<body>
<form id="form1" runat="server">
<h4>
This page uses post cache substitution to insert a dynamic value into a cached page.</h4>
<p>
Time:
<%= DateTime.Now.ToString() %>
</p>
<p>
<b>Real Time:
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
</b>
</p>
</form>
</body>
</html>
• 如果需要對緩存進行更詳細的設定,可以通過設定System.Web.HttpCachePolicy屬性來進行配置下面這個語句和我們<%@Output Cache Duration=“60” VaryByParam=“none”%>完全一樣
• Response.Cache.SetExpires(Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
二、控件緩存Page Fragment Caching
• 作為Output的緩存的附加功能,還提供一種緩存技術,專門用于緩存使用者控件。
• 在頁面中,通過指定傳回參數,來決定控件被緩存的部分。使用語句VaryByParam語句指定控件根據參數來改變。
• 通過傳回參數改變緩存内容示例
接受控件傳參數修改
• 緩存使用者控件同樣還可以使用控件作為參數來源。通過指定控件作為緩存控件的資料來源,可以達到緩存控件資料的目的
• 通過控件參數改變緩存内容示例
三、資料緩存(Cache對象)
• Asp.net提供了一種非常快捷的方法進行資料庫緩存,使用者可以非常友善的對頁面變量進行緩存。并以此提高程式效率。
• 一個頁面變量的緩存生命周期與應用程式的緩存生命周期相同。
• 同時在對背景資料進行修改的時候,還需要對cache進行相應的處理。
• 使用資料緩存示例
Cache.Insert(“Mydata”,dataset)
DataSet ds = Cache[“Mydata”]
四、資料庫依賴緩存SQL Caching
• 前面的例子中,我們使用的是緩存技術一旦時間到,無論伺服器端的資料是否被改變,都會釋放緩存。
• 下面我們介紹的例子,通過配置資料庫連接配接池,隻有當資料庫資料資料被改變的時候,緩存才會改變。
• 配置連接配接池示例
Cache 配置
• 可以通過在webconfig裡配置不同的緩存描述,在頁面中調用該描述來減少重複定義緩存描述的工作量。
<caching>
<sqlCacheDependency enabled="true" pollTime="1000" >
<databases>
<add name="PubsDB" connectionStringName="pubsConnectionString" />
</databases>
</sqlCacheDependency>
</caching>
• 緩存描述定義示例
1. 資料庫配置
aspnet_regsql指令在c:/windows/Microsoft.net/fframework/…
啟動緩存資料庫
aspnet_regsql –s localhost –u sa –p mypassword –d pubs –dd
啟動緩存表
aspnet_regsql –s localhost –u sa –p mypassword –d pubs –t titles –dt
2. 頁面配置
<%@ OutputCache Duration=”10000” SqlDependency=”pubs:titles” VaryByParam=”none” %>