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

ASP.NET缓存

2010-11-17 15:23 141 查看
一:ASP.NET中缓存分类,共3种

页面输出缓存、页面片段缓存、页面数据缓存

二:页面输出缓存

可以使用页面输出缓存来提高WEB站点的性能。可以缓存页面的输出并且把缓存起来的拷贝发送出去以响应浏览器的请求,而不是每次在请求页面时执行页面


例:你的站点包括一个从数据库表检索出来的显示产品信息的页面。默认情况下,每次用户访问产品页面时,都必须执行该页面并且从数据库检索数据。但如果启用页面输出缓存,这个页面就只执行一次,并且只从数据库检索一次数据。这就意味着减轻了WEB应用程序和数据库服务器的负载。

要启用页面输出缓存,在页面中包括如下页面处理指令即可:

<%@ OutputCache Duration="60"
VaryByParam="none" %>

如下例:

<%@ Page Language="C#" %>

<%@
OutputCache Duration="300" VaryByParam="none" %>

<%@ Import
Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient"
%>

<script language="C#"
runat="server">

void Page_Load(Object sender , EventArgs e)

{

SqlConnection conNorthwind;

string strSelect;

SqlCommand
cmdSelect;

SqlDataReader dtrProducts;

conNorthwind = new SqlConnection(
@"Server=localhost;Integrated Security=SSPI;Database=Northwind" );

strSelect
= "Select * From Products Where ProductID=1";

cmdSelect = new SqlCommand(
strSelect, conNorthwind );

conNorthwind.Open();

dgrdProducts.DataSource =
cmdSelect.ExecuteReader( CommandBehavior.SingleRow
);

dgrdProducts.DataBind();

conNorthwind.Close();

string strTime =
DateTime.Now.ToString();

lblMessage.Text =
strTime;

}

</script>

<html>

<head><title>ProductPage.aspx</title></head>

<body>

<asp:DataGrid

ID="dgrdProducts"

CellPadding="10"

Runat="Server"
/>

<hr>

<small>Last Updated:
<asp:Label

ID="lblMessage"

Font-Bold="True"

ForeColor="Blue"

Runat="Server"
/></small>

</body>

</html>

此例中显示了从Northwind数据库检索到的特定产品信息,这个页面被缓存5分钟。

注:
不能保证缓存的项总会保留在页面的输出缓存中。如果系统资源不足,
ASP.NET
框架会开始从缓存中清理缓存项。

属性
Duration
表示缓存的时间,以秒为单位。

属性
VaryByParam
表示对一个页面请求中包含参数
(
查询字符串参数或表单域参数
)
时页面该如何被缓存。这个属性可以接受分号作为分隔符的参数列表,或者是如下两个特殊值:

none------
表示即使页面被请求时带不同的参数,也不会创建页面不同的缓存版本。

*-----------
表示在页面被请求时带有不同的参数,则创建页面不同的缓存本。

如: http://locathost/p.aspx?pid=4 http://localhost/p.aspx?pid=5
<%@ OutputCache Duration="300"
VaryByParam="pid" %>

这条指令根据查询字符串参数
pid


设置缓存的位置
---
属性
Location(
取来自
OutputCacheLocation
枚举类型的一个值
)


Any---
默认值。页面可在任何地方缓存,包括服务器、下游服务器、浏览器。

Client---
页面的输出只在浏览器缓存。

DownStream----
页面的输出只在下游服务器上缓存。

None----
不执行页面缓存操作。

Server----
页面只在服务器端缓存。

使用
HttpCachePolicy

--
精确控制页面的缓存行为

其中
SetExpires()
方法表示页面过期的日期,
SetCacheability()
方法表示页面要如何来缓存。
SetCachability()
方法取
HttpCacheability
枚举类型中的值:

NoCache---
防止任何页面用
Cache-Control:no-cache
头来缓存。

Private--
表示页面不应当被代理服务器来缓存,但可以被浏览器缓存。默认值。

Public--
表示页面可以被浏览器和代理服务器缓存。

Server--
表示页面只可以在服务器端被缓存。

三:使用页面片段缓存

页面片段缓存是通过用户服务控件来实现的。通过为页面的每个区域创建一个单独的用户控件,就可以定义页面的不同区域。在每个用户控件中,可以使用
OutputCache
指令来表示如何缓存控件的输出。

注意:

在默认情况下,每个用户控件的输出都被单独缓存起来,如果要跨多个用户控件共享同样的输出缓存,以节省内在开销,那么可以使用
Shared
属性

<%@ OutputCache Duration="60"
VaryByParam="*" Shared="true" %>

页面片段缓存的限制:

由于控件被缓存了,就不能在页面中编程设置用户控件的属性;也不能对被缓存的用户控件使用数据绑定的语法。

四:使用页面数据缓存

每个ASP.NET应用程序都有一个Cache对象,直到应用程序重新启动之前该对象一直可用。

Cache.Insert("t1",
"goodtyl");//
新增项

Cache.Insert("t2", "weiwei");

Cache.Insert("t3", "haha");

Cache.Remove("t3");//移除项

//遍历Cache,注意System.Collections.DictionaryEntry:定义可设置或可检索的字典键/值对

foreach(DictionaryEntry temp in Cache)

{

Response.Write("key: " + temp.Key.ToString() + "value: " + temp.Value.ToString()
+ "<br>");

}

添加缓存文件依赖
:在添加一个文件依赖时,可以把该项与一个文件相关联。如果文件发生改变,就自动从缓存中删除该项。如例:Cache.Insert("mi","hello",new
System.Web.Caching.CacheDependency(MapPath("a.txt")));如果a.txt被修改了,那么,mi就自动从缓存中删除。

添加缓存触发依赖
:只要数据库表的记录发生改变就更新缓存中的一项。

需要使用sqlserver触发器及xp_cmdshell扩展存储过程。

CREATE TRIGGER UpdateCache

ON
Products

FOR UPDATE, DELETE, INSERT

AS

DECLARE @cmd Varchar( 200
)

SELECT @cmd = 'echo ' + Cast( getDATE() As Varchar( 50 ) ) +

' >
c:/tableChange.txt'

EXEC master..xp_cmdshell @cmd, no_output

代码:

<%@ Page Language="C#" %>

<%@
Import Namespace="System.Data" %>

<%@ Import
Namespace="System.Data.SqlClient" %>

<script language="C#"
runat="server">

void Page_Load(Object sender , EventArgs e)

{

DataSet dstProducts = (DataSet)(Cache[ "productsDS" ]);

if (
dstProducts == null )

{

dstProducts = GetProducts();

Cache.Insert( "Products", dstProducts, new CacheDependency(
"c://tableChange.txt" ) );

}

dgrdProducts.DataSource =
dstProducts;

dgrdProducts.DataBind();

}

DataSet GetProducts() {

SqlConnection
conNorthwind;

string strSelect;

SqlDataAdapter dadProducts;

DataSet dstProducts;

conNorthwind = new SqlConnection(
@"Server=localhost;Integrated Security=SSPI;Database=Northwind" );

strSelect
= "Select TOP 20 * From Products ORDER BY ProductID";

dadProducts = new
SqlDataAdapter( strSelect, conNorthwind );

dstProducts = new
DataSet();

dadProducts.Fill( dstProducts, "ProductsDS" );

return
dstProducts;

}

</script>

<html>

<head><title>DatabaseDependency.aspx</title>

</head>

<body>

<asp:DataGrid

ID="dgrdProducts"

Runat="Server"
/>

</body>

</html>

添加缓存键依赖


<%@ Page Language="C#" %>

<script
language="C#" runat="server">

void UpdateItem1( object s, EventArgs e
)

{

Cache[ "item1" ] = txtNewValue.Text;

}

void UpdateItem2( object s, EventArgs e
)

{

string arrKeyDepends = ( "item1" );

Cache.Insert( "item2",
txtNewValue.Text, new CacheDependency( Server.MapPath(arrKeyDepends),
System.DateTime.Now ) );

}

</script>

<html>

<head><title>KeyDependency.aspx</title></head>

<body>

<form
Runat="Server">

<asp:TextBox

ID="txtNewValue"

Runat="Server"
/>

<p>

<asp:Button

Text="Update
Item1"

OnClick="UpdateItem1"

Runat="Server"
/>

<asp:Button

Text="Update
Item2"

OnClick="UpdateItem2"

Runat="Server" />

<hr>

Item1 = <%=Cache[ "item1"
]%>

<br>

Item2 = <%=Cache[ "item2" ]%>

</form>

</body>

</html>

创建一个绝对过期策略


Cache.Insert("Time", strTime, null,
DateTime.Now.AddMinutes( 1 ), Cache.NoSlidingExpiration );

创建相对过期策略


Cache.Insert("Time", strTime, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes( 1 ) );

设置缓存项的优先级


Cache.Insert("my","hello",null,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,CacheItemPriority.High,null);

创建缓存回调方法
:在插入一个项到缓存时,可以把一个回调方法与这个项关联起来。无论是何种原因,只要该项从缓存中删除,那么回调方法就自动执行。

最明显的回调应用是当一个项过期时自动把该项重新加载到缓存中。

示例代码:

<%@ Page Language="C#" %>

<script
language="C#" runat="server">

public static CacheItemRemovedCallback
onRemove;

//CacheItemRemovedReason
表示从缓存中删除项的原因

void ItemRemoved( string strItemKey,
object objItemValue, CacheItemRemovedReason objRemovedReason )

{

string
strLogEntry;

strLogEntry = "Item with value " +
objItemValue.ToString() ;

strLogEntry = " removed at " +
System.DateTime.Now.ToString( "T" );

strLogEntry = " because of " +
objRemovedReason.ToString();

if ( Application[ "CacheLog" ] == null
)

{

Application[ "CacheLog" ] = new ArrayList();

}

//Application[
"CacheLog" ].Add( "strLogEntry" );

//Beep();

}

void btnAddCache_Click( object s, EventArgs e
)

{

onRemove = new CacheItemRemovedCallback( ItemRemoved );

Cache.Insert( "myItem", txtNewValue.Text, null, DateTime.Now.AddSeconds( 10 ),
Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove );

}

void btnRemoveCache_Click( object s, EventArgs
e )

{

Cache.Remove( "myItem" );

}

void Page_PreRender( object s, EventArgs e
)

{

dgrdCacheLog.DataSource = Application[ "CacheLog"
];

dgrdCacheLog.DataBind();

}

</script>

<html>

<head><title>CacheCallback.aspx</title></head>

<body>

<form
Runat="Server">

<h2>Cache
Log</h2>

<asp:DataGrid

ID="dgrdCacheLog"

CellPadding="8"

Runat="Server"
/>

<p>

<asp:TextBox

id="txtNewValue"

Runat="Server"
/>

<p>

<asp:Button

id="btnAddCache"

Text="Add To
Cache!"

OnClick="btnAddCache_Click"

Runat="Server" />

<asp:Button

id="btnRemoveCache"

Text="Remove
From Cache!"

OnClick="btnRemoveCache_Click"

Runat="Server"
/>

<asp:Button

Text="Refresh
Page!"

Runat="Server" />

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: