欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET緩存介紹

 更新時間:2012年04月04日 19:30:15   作者:  
緩存是在內(nèi)存存儲數(shù)據(jù)的一項(xiàng)技術(shù),也是ASP.NET中提供的重要特性之一。例如你可以在復(fù)雜查詢的時候緩存數(shù)據(jù),這樣后來的請求就不需要從數(shù)據(jù)庫中取數(shù)據(jù),而是直接從緩存中獲取。通過使用緩存可以提高應(yīng)用程序的性能

ASP.NET緩存

介紹

緩存是在內(nèi)存存儲數(shù)據(jù)的一項(xiàng)技術(shù),也是ASP.NET中提供的重要特性之一。例如你可以在復(fù)雜查詢的時候緩存數(shù)據(jù),這樣后來的請求就不需要從數(shù)據(jù)庫中取數(shù)據(jù),而是直接從緩存中獲取。通過使用緩存可以提高應(yīng)用程序的性能。

主要有兩種類型的緩存:

輸出緩存Output caching\

數(shù)據(jù)緩存Data caching

1. 輸出緩存(Output Caching)

使用輸出緩存,你可以緩存最后輸出的HTML頁面,當(dāng)相同的頁面再次請求的時候,ASP.NET不會再執(zhí)行頁面的生命周期和相關(guān)代碼而是直接使用緩存的頁面,語法如下:

復(fù)制代碼 代碼如下:

<%@ OutputCache Duration=”60” VaryByParam=”None” %>

Duration 屬性設(shè)置頁面將被緩存60妙。任何的用戶請求都會被緩存,在緩沖的60秒內(nèi)相同的請求都會直接使用緩存的頁面。當(dāng)緩存過期后ASP.NET會再次執(zhí)行頁面代碼并且為下一個60秒創(chuàng)建一個新的HTML緩存。
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="OutputCachingTest.aspx.cs" Inherits="OutputCachingTest" Title="Page" %>
<%@ OutputCache Duration="20" VaryByParam="None" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="title">Output Cache</div>
Date: <asp:Label ID="lblDate" runat="server" Text="" />
Time: <asp:Label ID="lblTime" runat="server" Text="" />
</asp:Content>
protected void Page_Load(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToShortDateString();
lblTime.Text = DateTime.Now.ToLongTimeString();
}


在這個例子中頁面將被緩存20秒。

通過查詢字符串緩存(Cache by Query String )

在實(shí)際應(yīng)用中頁面往往會根據(jù)一些參數(shù)動態(tài)的改變頁面的內(nèi)容。如果你的頁面是通過查詢字符串來獲取信息的,你可以根據(jù)查詢字符串很容易的緩存頁面的不同拷貝。VarByParam=”None”指定ASP.NET只存儲緩存頁面的一個拷貝。VarByParam=”*” 指定ASP.NET根據(jù)不同的查詢字符串存儲不同的緩存頁面。
復(fù)制代碼 代碼如下:

<%@ OutputCache Duration="60" VaryByParam="*" %>
<div align="right">
<a href="OutputCachingTest2.aspx">No Query String</a> |
<a href="OutputCachingTest2.aspx?id=1">ID 1</a> |
<a href="OutputCachingTest2.aspx?id=2">ID 2</a> |
<a href="OutputCachingTest2.aspx?id=3">ID 3</a> |
<a href="OutputCachingTest2.aspx?id=3&langid=1">ID 3</a>
</div>

上面的例子中,在查詢字符串中傳了不同的ID.ASP.NET為每一個ID都存儲了單獨(dú)的緩存頁面。這種方式會有一些問題就是當(dāng)查詢字符串范圍很廣的時候。
這個時候我們可以在VarByParam 屬性中指定重要的查詢字符串變量的名字,如下:
復(fù)制代碼 代碼如下:

%@OutputCacheDuration="60"VaryByParam="id;langid"%

自定義緩存(Custom Caching)
你也可以創(chuàng)建自定義的程序來緩存頁面。ASP.NET提供了一種很便捷的方式來創(chuàng)建自定義緩存,使用VarByCustom屬性指定自定義緩存類型的名字。
你還要創(chuàng)建為緩存生成自定義字符串的方法,如下:
復(fù)制代碼 代碼如下:

public override stringGetVaryByCustomString(HttpContext context, stringcustom)
{
if(custom == "browser")
{
returncontext.Request.Browser.Browser +
context.Request.Browser.MajorVersion;
}
else
{
return base.GetVaryByCustomString(context, custom);
}
}


這個方法必須寫在global.asax文件中。ASP.NET使用該方法返回的字符串來實(shí)現(xiàn)緩存,如果這個方法在不同的請求中返回相同的字符串,ASP.NET就會使用緩存的頁面,否則就會生成新的緩存版本。
上面的例子中GetVaryByCustomString()方法根據(jù)瀏覽器的名字創(chuàng)建緩存字符串,ASP.NET會根據(jù)不同的瀏覽器請求創(chuàng)建不同版本的緩存。
控件緩存(Control Cache )
上面的緩存技術(shù)可以讓你很容易的緩存整個頁面,如果要緩存指定控件的內(nèi)容,可以通過指定VaryByControl 屬性來完成。
復(fù)制代碼 代碼如下:

%@OutputCacheDuration="20"VaryByControl="MyControl_1"%

上面代碼ASP.NET將會緩存MyControl_1控件20分鐘。如果要根據(jù)一些屬性值來緩存控件只需要將OutPutCache指令加入*.ascx頁面。
復(fù)制代碼 代碼如下:

<%@Control Language="C#"AutoEventWireup="true"CodeFile="MyControl.ascx.cs"
Inherits="Controls_MyControl"%>
<%@OutputCacheDuration="20"VaryByControl="EmployeeID"%>

VaryByControl=”EmployeeID”告訴ASP.NET根據(jù)控件中聲明的EmployeeID屬性來創(chuàng)建不同版本的緩存。
在 .ascx.cs 文件加入EmplyeeID屬性為ASP.NET 緩存使用。
在頁面中增加控件并且設(shè)置 EmployeeID.
復(fù)制代碼 代碼如下:

private int_employeeID;
public intEmployeeID
{
get{ return_employeeID; }
set{ _employeeID = value; }
}
protected voidPage_Load(objectsender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToShortDateString();
lblTime.Text = DateTime.Now.ToLongTimeString();
lblEmployeeID.Text = EmployeeID.ToString();
}

緩存配置文件(Cache Profile )
web.config可以配置緩存相關(guān)的設(shè)置,
復(fù)制代碼 代碼如下:

<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<addname="ProductItemCacheProfile" duration="60"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

你可以通過設(shè)置 CacheProfile=”ProfileName” 屬性 來使用上面的配置:
復(fù)制代碼 代碼如下:

%@OutputCacheCacheProfile="ProductItemCacheProfile"VaryByParam="None"%

2. 數(shù)據(jù)緩存(Data Caching)
ASP.NET還提供了另一種靈活的緩存類型:數(shù)據(jù)緩存。你可以將一些耗費(fèi)時間的條目加入到一個對象緩存集合中,以鍵值的方式存儲。
復(fù)制代碼 代碼如下:

Cache["Name"] = data;


我們可以通過使用Cache.Insert()方法來設(shè)置緩存的過期,優(yōu)先級,依賴項(xiàng)等。
復(fù)制代碼 代碼如下:

date1 = DateTime.Now;Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);


ASP.NET允許你設(shè)置一個絕對過期時間或滑動過期時間,但不能同時使用。
緩存依賴項(xiàng)Cache dependency
緩存依賴項(xiàng)使緩存依賴于其他資源,當(dāng)依賴項(xiàng)更改時,緩存條目項(xiàng)將自動從緩存中移除。緩存依賴項(xiàng)可以是應(yīng)用程序的 Cache 中的文件、目錄或與其他對象的鍵。如果文件或目錄更改,緩存就會過期。
復(fù)制代碼 代碼如下:

date2 = DateTime.Now;
string[] cacheKeys = { "Date1"};
CacheDependency cacheDepn = newCacheDependency(null, cacheKeys);
Cache.Insert("Date2", date2, cacheDepn);


上面的例子“Date2”緩存對象依賴“Date1”緩存條目,當(dāng) “Date1” 對象過期后“Date2” 將會自動過期。CacheDependency(null, cacheKeys)中的第一個參數(shù)為空是由于我們只監(jiān)視緩存鍵的更改情況。
回調(diào)函數(shù)和緩存優(yōu)先級(Callback Method and Cache Priority)
ASP.NET允許我們寫一個回調(diào)函數(shù),當(dāng)緩存條目從緩存中移除的時候觸發(fā)。還可以設(shè)置緩存條目的優(yōu)先級。
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
DateTime? date1 = (DateTime?)Cache["Date1"];
if (!date1.HasValue) // date1 == null
{
date1 = DateTime.Now;
Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero,
CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
}
DateTime? date2 = (DateTime?)Cache["Date2"];
if (!date2.HasValue) // date2 == null
{
date2 = DateTime.Now;
Cache.Insert("Date2", date2, null, DateTime.Now.AddSeconds(40), TimeSpan.Zero,
CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
}
// Set values in labels
lblDate.Text = date1.Value.ToShortDateString();
lblTime.Text = date1.Value.ToLongTimeString();
lblDate1.Text = date2.Value.ToShortDateString();
lblTime1.Text = date2.Value.ToLongTimeString();
}
private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
{
if (key == "Date1" || key == "Date2")
{
Cache.Remove("Date1");
Cache.Remove("Date2");
}
}


例子中創(chuàng)建了“Date1” 和 “Date2”緩存?!癉ate1” 在20秒后過期“Date2”為40秒。但是由于我們注冊了移除的回調(diào)函數(shù),當(dāng)“Date1” 或 “Date2”其中一個過期都會執(zhí)行CachedItemRemoveCallBack 方法,在這個方法中移除了兩個緩存條目,ASP.NET還提供了處理緩存條目更新時的回調(diào)函數(shù)CacheItemUpdateCallback 。
原文:http://kb.cnblogs.com/page/50971/

相關(guān)文章

最新評論