|
介紹
緩存是在內(nèi)存存儲(chǔ)數(shù)據(jù)的一項(xiàng)技術(shù),也是ASP.NET中提供的重要特性之一。例如你可以在復(fù)雜查詢的時(shí)候緩存數(shù)據(jù),這樣后來的請求就不需要從數(shù)據(jù)庫中取數(shù)據(jù),而是直接從緩存中獲取。通過使用緩存可以提高應(yīng)用程序的性能。
主要有兩種類型的緩存:
1.輸出緩存Output caching
2.數(shù)據(jù)緩存Data caching
1. 輸出緩存(Output Caching)
使用輸出緩存,你可以緩存最后輸出的HTML頁面,當(dāng)相同的頁面再次請求的時(shí)候,ASP.NET不會(huì)再執(zhí)行頁面的生命周期和相關(guān)代碼而是直接使用緩存的頁面,語法如下:
<%@ OutputCache Duration=”60” VaryByParam=”None” %>
Duration
屬性設(shè)置頁面將被緩存60妙。任何的用戶請求都會(huì)被緩存,在緩沖的60秒內(nèi)相同的請求都會(huì)直接使用緩存的頁面。當(dāng)緩存過期后ASP.NET會(huì)再次執(zhí)行頁面代碼并且為下一個(gè)60秒創(chuàng)建一個(gè)新的HTML緩存。
<%@ 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();}
在這個(gè)例子中頁面將被緩存20秒。
通過查詢字符串緩存(Cache by Query String )
在實(shí)際應(yīng)用中頁面往往會(huì)根據(jù)一些參數(shù)動(dòng)態(tài)的改變頁面的內(nèi)容。如果你的頁面是通過查詢字符串來獲取信息的,你可以根據(jù)查詢字符串很容易的緩存頁面的不同拷貝。VarByParam=”None”
指定ASP.NET只存儲(chǔ)緩存頁面的一個(gè)拷貝。VarByParam=”*”
指定ASP.NET根據(jù)不同的查詢字符串存儲(chǔ)不同的緩存頁面。
<%@ 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為每一個(gè)ID都存儲(chǔ)了單獨(dú)的緩存頁面。這種方式會(huì)有一些問題就是當(dāng)查詢字符串范圍很廣的時(shí)候。
這個(gè)時(shí)候我們可以在VarByParam
屬性中指定重要的查詢字符串變量的名字,如下:
<%@OutputCacheDuration="60"VaryByParam="id;langid"%>
這樣,ASP.NET可以根據(jù)id” or “langid”來緩存不同的緩存版本。
自定義緩存(Custom Caching)
你也可以創(chuàng)建自定義的程序來緩存頁面。ASP.NET提供了一種很便捷的方式來創(chuàng)建自定義緩存,使用VarByCustom
屬性指定自定義緩存類型的名字。
你還要?jiǎng)?chuàng)建為緩存生成自定義字符串的方法,如下:
public override stringGetVaryByCustomString(HttpContext context, stringcustom){ if(custom == "browser") { returncontext.Request.Browser.Browser + context.Request.Browser.MajorVersion; } else { return base.GetVaryByCustomString(context, custom); }}
這個(gè)方法必須寫在global.asax
文件中。ASP.NET使用該方法返回的字符串來實(shí)現(xiàn)緩存,如果這個(gè)方法在不同的請求中返回相同的字符串,ASP.NET就會(huì)使用緩存的頁面,否則就會(huì)生成新的緩存版本。
上面的例子中GetVaryByCustomString()
方法根據(jù)瀏覽器的名字創(chuàng)建緩存字符串,ASP.NET會(huì)根據(jù)不同的瀏覽器請求創(chuàng)建不同版本的緩存。
控件緩存(Control Cache )
上面的緩存技術(shù)可以讓你很容易的緩存整個(gè)頁面,如果要緩存指定控件的內(nèi)容,可以通過指定VaryByControl
屬性來完成。
<%@OutputCacheDuration="20"VaryByControl="MyControl_1"%>
上面代碼ASP.NET將會(huì)緩存MyControl_1控件
20分鐘。如果要根據(jù)一些屬性值來緩存控件只需要將OutPutCache指令加入*.ascx
頁面。
<%@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
.
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è)置,
<system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <addname="ProductItemCacheProfile" duration="60"/> </outputCacheProfiles> </outputCacheSettings> </caching></system.web>
你可以通過設(shè)置 CacheProfile=”ProfileName”
屬性 來使用上面的配置:
<%@OutputCacheCacheProfile="ProductItemCacheProfile"VaryByParam="None"%>
NET技術(shù):ASP.NET緩存,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。