ASP.NET 开发实践--性能与缓存
1.使用IsPostBack屬性避免不必要的執(zhí)行操作
?
代碼 //只要有用花交互就會(huì)執(zhí)行這個(gè)事件void Page_Load(Object sender, EventArgs e) {
if (!Page.IsPostBack) {
String query = "select * from Authors where FirstName like '%JUSTIN%'";
myCommand.Fill(ds, "Authors");
myDataGrid.DataBind();
}
}
?
2.關(guān)閉不必要的Session狀態(tài)(當(dāng)用戶在應(yīng)用程序的 Web 頁(yè)之間跳轉(zhuǎn)時(shí),存儲(chǔ)在?Session?對(duì)象中的變量將不會(huì)丟失)
?
<%@ Page EnableSessionState="false" %>?
3.注意使用Server Control
不必要時(shí)(不需要用戶交互)可以不使用Server Control不必要時(shí)可以關(guān)閉ViewState(生命周期僅為當(dāng)前頁(yè)面)
EnableViewState設(shè)置為false 當(dāng)回發(fā)時(shí)控件的狀態(tài)會(huì)恢復(fù)到aspx頁(yè)的初始設(shè)置
<asp:datagrid EnableViewState="false“ runat="server"/>?
//關(guān)閉整個(gè)頁(yè)面的ViewState
<%@ Page EnableViewState="false"%>
4.不要用Exception控制程序流程
5.禁用VB和JScript動(dòng)態(tài)數(shù)據(jù)類型
<%@ Page Language="VB" Strict="true" %>6.盡量使用存儲(chǔ)過(guò)程數(shù)據(jù)訪問(wèn)
7.只讀數(shù)據(jù)訪問(wèn)不要使用DataSet
使用SqlDataReader代替DataSetSqlDataReader是read-only, forward-only
8.關(guān)閉ASP.NET的Debug模式
9.使用ASP.NET Output Cache緩沖數(shù)據(jù)
?
<%@ OutputCache Duration ="120" VaryByParam ="TextBox1;TextBox2" %>?
Duration:緩沖時(shí)間 VaryByParam :當(dāng)TextBox1和TextBook2中值的組合改變會(huì)增加Cache 如果設(shè)置為None 則無(wú)論TextBox1及TextBox2值是否改變頁(yè)面都會(huì)使用緩存值,不會(huì)更新
?
VaryByControl:可把用戶控件中應(yīng)用<%@ OutputCache %>指令 具體參見(jiàn)OutputCache
?
10.數(shù)據(jù)緩沖Cache
?
代碼 Cache.Insert("MyData", Source, new CacheDependency(Server.MapPath("authors.xml")));Cache.Insert("MyData", Source, null,
DateTime.Now.AddHours(1), TimeSpan.Zero);
Cache.Insert("MyData", Source, null, DateTime.MaxValue,
TimeSpan.FromMinutes(20));
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/hahacjh/archive/2010/06/02/1750242.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET 开发实践--性能与缓存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .net framework 4.0环境
- 下一篇: 如何向mysql导入数据库(。sql文件