新手入門 ASP.NET2.0緩衝技術
2007-12-13 09:43 來源:部落格網
ASP.NET2.0提供如下緩衝方式:
Output Caching
Fragment Caching
Data Cache
SQL Cache
Cache Configuration
1. Output Caching:
當一個網頁被頻繁訪問時,我們可以把把整個網頁緩衝起來提高效率,當使用者在此訪問時,被格式化好的HTML被直接送到用戶端。
<%@ OutputCache Duration="120" VaryByParam="none" %>
2. 參數緩衝:
根據使用者的請求來產生頁面,使用者的請求只有有限的幾種組合,我們根據參數該表緩衝內容。
<%@ OutputCache Duration="120" VaryByParam="state" %>
<%--<a href="Default.aspx?state=CA"></a>--%>
3. 硬碟緩衝:
預設情況下Output Cache會緩衝到硬碟上,我們可通過修改diskcacheenable的屬性設定其是否緩衝,還可以通過在web config裡配置快取檔案的大小。
4. 頁面片段緩衝:
頁面上部分內容根據請求動態更新,大部分能容被緩衝。(如果多個控制項需要緩衝,可做成一個使用者控制項)
<%@OutputCache Duration="120" VaryByControl="ControlID" %>
<center><img src="http://myarticle.enet.com.cn/images/2007/1213/1197510156064.jpg" border="0" alt="新手入門 ASP.NET2.0緩衝技術"/></center>
5. Cache Data :
建議開啟硬碟緩衝,緩衝時間設的稍長一點,因為IO的開銷
DataSet ds=new DataSet();
ds = Cache["restaurant"];
if (ds == null)
{
ds = resDataSet;
Cache["restaurant"] = ds;
}
6. SQL Dependency
設定資料庫伺服器的sql緩衝,然後在頁面引用
<center><img src="http://myarticle.enet.com.cn/images/2007/1213/1197510159540.jpg" border="0" alt="新手入門 ASP.NET2.0緩衝技術"/></center>
7. Cache Configuration (減少重複定義)
a .web.config定義
<center><img src="http://myarticle.enet.com.cn/images/2007/1213/1197510164294.jpg" border="0" alt="新手入門 ASP.NET2.0緩衝技術"/></center>
b. 頁面調用
<%@ OutputCache CacheProfile="CacheFor60Seconds" VaryByParam="name" %>
<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr> <td><b>熱門推薦</b>:</td>
<td><a href="http://www.enet.com.cn/article/2007/1210/A20071210942740.shtml" target="_blank"><font size=2 color=red>無敵命令 刪除不能刪除的檔案
</font></a></td>
<td><a href="http://www.enet.com.cn/article/2007/1211/A20071211943925.shtml" target="_blank"><font size=2 color=red>不怕被攻擊 Windows防黑技巧七招</font></a></td>
</tr></table>