您的位置:首页 > 编程语言 > ASP

ASP.NET 中的缓存功能介绍

2006-08-18 15:47 344 查看

ASP.NET 中的缓存功能介绍

ASP.NET的性能状况

ASP是脚本解释执行的

ASP.NET的引擎从原理上保证高性能 一次编译

ASP.NET的性能状况


ASP.NET的引擎从原理上保证高性能


CLR Just-in-time Compiler


对多CPU有很好的支持


运行时优化编译


引擎的优化不能彻底解决性能问题


代码逻辑不优化


引擎无法控制的,潜在的性能优化点

性能问题与优化原则


性能参数


吞吐量:绝大部分测试都是以吞吐量为标准,在评测中我们考虑用户连接数。


响应时间


执行时间


可伸缩性


基本优化原则


减少不必要的资源消耗CPU,内存

性能提高的技巧


避免不必要的执行操作


Page_Load 和 IsPostBack

Page_Load

Properties_Change

Action

Asp.net服务器段执行的事情请求顺序。其中Properties_Change事件包括(比如TextBox的TextChanged事件,这种事件如果被频繁引发,将加重服务器端的负荷,在Asp.net中这种事件,一般将和Action事件一起发送到服务器),Action事件(比如Button的Click事件)

void Page_Load(Object sender, EventArgs e)

{

// ...set up a connection and command here...

if (!Page.IsPostBack)

{

String query = "select * from Authors where FirstName like '%JUSTIN%'";

myCommand.Fill(ds, "Authors");

myDataGrid.DataBind();

}

}

void Button_Click(Object sender, EventArgs e)

{

String query = "select * from Authors where FirstName like '%BRAD%'";

myCommand.Fill(ds, "Authors");

myDataGrid.DataBind();

}


关闭不必要的Session状态


<%@ Page EnableSessionState="false" %>


注意使用Server Control


不必要时可以不使用Server Control


不必要时可以关闭ViewState


<asp:datagrid EnableViewState="false“ runat="server"/>


<%@ Page EnableViewState="false" %>


不要用Exception控制程序流程

try

{

result = 100 / num;

}

catch (Exception e)

{

result = 0;

}

这样的执行是比较耗时的,可以简单应用下面的

if (num != 0)

result = 100 / num;

else

result = 0;


禁用VB和JScript动态数据类型


<%@ Page Language="VB" Strict="true" %>


使用存储过程数据访问


只读数据访问不要使用DataSet


使用SqlDataReader代替DataSet


SqlDataReader是read-only, forward-only


关闭ASP.NET的Debug模式


使用ASP.NET Output Cache缓冲数据

ASP.NET输出缓冲


页面缓冲


<%@OutputCache%>


Duration


VaryByParam:由于输入参数的不同,每个OutputCache版本也不同

<%@OutputCache Duration=60 VaryByParam =”TextBox(控件名)”%>


片断缓冲


VaryByControl


上面页面缓冲是对于整个页面而言的,在更多的时候,我们是把需要缓冲的控件做成一个用户控件,并打上OutputCache标记。但是这时候有一个比较严重的情况,假设在页面上有同一用户空间的不同实现对象,在页面加载的时候,当用户控件的一个实现完成时,第二个用户控件将应用第一个的Cache,这时候我们就需要设置VaryByControl

ASP.NET输出缓冲


数据缓冲


过期依赖条件

ASP.NET 还提供Cache对象,具体我们参考MSDN

Cache.Insert("MyData", Source, new CacheDependency(Server.MapPath("authors.xml")));

Cache.Insert("MyData", Source, null, DateTime.Now.AddHours(1), TimeSpan.Zero);

//当Cache没有重写的时候,20分钟后将过期;如果重写,时间将重新开始

Cache.Insert("MyData", Source, null, DateTime.MaxValue,TimeSpan.FromMinutes(20));

ASP.NET 1.0的缓存机制

1. 缓存对应用程序的性能具有最大的影响

尽早缓存,经常缓存

缓存可以防止许多过失

2. ASP.NET提供主要形式的缓存

页面缓存(OutputCache指令)

片段缓存(用户控件输出缓存)

3. 数据过期问题,特别是数据库驱动的应用程序,在数据库的数据更改时。

ASP.NET 2.0中新增的缓存功能

l 新增的DataSource控件

sqlDataSource AccessDataSource ObjectDataSource

使用DataSource控件缓存数据

默认情况下,sqlDataSource使用绝对过期策略来缓存数据。此外,你还可以选择使用可变过期策略。可以通过属性的选择CacheExpirationPolicy来改变过期策略,如果是Sliding就是没CacheDuration时间就刷新数据,默认情况是absolute。

[参考代码]

<asp:DropDownList ID="DropDownList1" DataSourceId="SqlDataSource1" DataTextField="title" Runat="server" />

//Cache过期时间,这里表示每隔300秒刷新数据

<asp:SqlDataSource ID="SqlDataSource1" EnableCaching="True" CacheDuration="300" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"

SelectCommand="SELECT * FROM [titles]" Runat="server" />

l Substitution控件

l SQL Cache Invalidation

解决ASP.NET1.0中数据过期的问题

ASP.NET 2.0中新增的缓存功能

l 使用Post-Cache Substitution的场合

ASP.NET1.0使用用户控件缓存。

我们的需求是在缓存页面的内容情况下动态添加内容。

l Substitution控件:使用Substitution控件,可以在缓存的页面中插入动态内容。

//页面缓存的内容,在页面过期之间内容不会改变

<p>Time:<%= DateTime.Now.ToString() %></p>

//动态缓存的内容,没刷新一次内容就及时更新

<b>Real Time:

<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />

</b>

<script runat="server">

Shared Function GetCurrentDate(ByVal context As HttpContext) As String

Return Now.ToString()

End Function

</script>

ASP.NET 2.0中新增的缓存功能

l SQL Cache Invalidation

对数据的任何更新,都能马上在缓存变化,而不必等到缓存时间到期

l 功能实现步骤(只能运行于SQL SERVER7.0以上版本)

1. 配置SQL Server支持SQL Cache Invalidation

命令状态下进入cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\

aspnet_regsql -S slhsql2005-1 -E -d pubs -ed

aspnet_regsql -S slhsql2005-1 -E -d pubs -t authors –et

具体含义:

aspnet_regsql命令帮助(-S -E 要大写)

-S 连接的数据库服务器

-E 使用身份认证方式(Windows集成认证)

-d 使用的数据库

-ed 为这个数据库启用SQL Cache Invalidation

-t 使用的表

-et 为这个数据库启用SQL Cache Invalidation

2. 编写ASP.Net程序代码使用SQL Cache Invalidation

页面配置:定义页面缓存输出时间600秒 pubs:authors大小写敏感

<%@ OutputCache Duration="600" varybyparam="none" sqldependency="pubs:authors"%>

//定义PageLoad事件,这里只是为了演示效果。这样数据库一更新,时间和DataView都进行更//新,而不用等到过期时间。

Protected Sub Page_Load(ByVal sender As Object,

ByVal e As System.EventArgs) Handles Me.Load

Label1.Text = CType(System.DateTime.Now(), String)

End Sub

3. 配置Web.config

在<system.web>配置,pubsConnectionString对应数据库连接字符串

<caching>

<sqlCacheDependency enabled="true" pollTime="1000" >

<databases>

<add name="pubs" connectionStringName="pubsConnectionString" />

</databases>

</sqlCacheDependency>

</caching>

在<configuration>中配置

<connectionStrings>

<add name="pubsConnectionString" connectionString="Data Source=.;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />

</connectionStrings>

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=908970
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: