一、緩存概念,緩存的好處、類型。
-------------------------------------------------------------------------------- 緩存是一種用空間換取時(shí)間的技術(shù),通俗點(diǎn)也就是說把你得到的數(shù)據(jù)存放在內(nèi)存中一段時(shí)間,在這短時(shí)間內(nèi)服務(wù)器不去讀取數(shù)據(jù)庫、或是真實(shí)的數(shù)據(jù)源,而是讀取你存放在內(nèi)存中的數(shù)據(jù),這里你會(huì)疑惑怎么設(shè)置存放數(shù)據(jù),能存放什么樣子的數(shù)據(jù),存放時(shí)間的設(shè)置,真實(shí)數(shù)據(jù)源數(shù)據(jù)改變服務(wù)器不就讀取存在偏差?別急,下面慢慢會(huì)說到的。。
緩存的好處,緩存是網(wǎng)站性能優(yōu)化不可缺少的一種數(shù)據(jù)處理機(jī)制,他能有效的緩解數(shù)據(jù)庫壓力,例如,網(wǎng)站每分鐘的點(diǎn)擊率為100萬,如果不使用緩存的靜態(tài)頁面,這里也沒有viewstate的情況下(viewstate會(huì)產(chǎn)生大量的字符串,對(duì)服務(wù)器交互數(shù)據(jù)是一種壓力,所以一般頁面是要禁用viewstate,采用緩存的),只能是用戶點(diǎn)擊一次該頁面,這個(gè)頁面就讀取一次數(shù)據(jù)庫,這樣給數(shù)據(jù)庫造成的壓力可想而知,如果這里我們使用了緩存的話,設(shè)置緩存有效期為1分鐘,則這一分鐘只內(nèi),點(diǎn)擊100萬次跟點(diǎn)擊一次是一樣的,都是讀取一次數(shù)據(jù)庫,數(shù)據(jù)源是被緩存在內(nèi)存中了。
asp.net中的緩存主要分為:頁面緩存,數(shù)據(jù)源緩存,自定義數(shù)據(jù)緩存這三種主要類型。
--------------------------------------------------------------------------------
二、數(shù)據(jù)緩存
--------------------------------------------------------------------------------
if (Cache["date"] == null) //判斷是否存在value值為date的緩存是否存在
{
Cache["date"] = datastr;
Response.Write("第二個(gè)輸出時(shí)間為:"+Cache["date"] + "這里讀取的當(dāng)前的時(shí)間"); //這里讀取的當(dāng)前的時(shí)間,刷新頁面時(shí),這里的時(shí)間會(huì)隨著變化。
}
else
{
Response.Write(Cache["date"] + "這里是從緩存中讀取的時(shí)間");//這里讀取的緩存中的時(shí)間,刷新頁面時(shí),這里的隨著時(shí)間變化,不會(huì)變化。
}
}
}
下面我們給數(shù)據(jù)緩存添加一些實(shí)用的參數(shù)(上代碼)。
--------------------------------------------------------------------------------
三、頁面緩存
--------------------------------------------------------------------------------
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
div>
/div>
/body>
/html>
ASP.NET不會(huì)再執(zhí)行頁面的生命周期和相關(guān)代碼而是直接使用緩存的頁面,簡單點(diǎn)理解也就是我注釋中介紹的。
--------------------------------------------------------------------------------
四、控件緩存
--------------------------------------------------------------------------------
1.ObjectDataSource這樣的數(shù)據(jù)源控件,可以在屬性欄中找到相應(yīng)的屬性,進(jìn)行設(shè)置,下面我列出個(gè)例子,設(shè)置啟動(dòng)緩存,緩存時(shí)間為10秒,時(shí)間類型為絕對(duì)時(shí)間。
asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnableCaching="True" CacheDuration="10" CacheExpirationPolicy="Absolute">/asp:ObjectDataSource>
2.沒有緩存屬性的控件要加緩存
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
div>
asp:TextBox ID="TextBox1" runat="server">/asp:TextBox>
/div>
/form>
/body>
/html>
五、緩存依賴
--------------------------------------------------------------------------------
}
六、配置文件中設(shè)置緩存
--------------------------------------------------------------------------------
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
div>
/div>
/body>
/html>
七、緩存的回調(diào)函數(shù)
--------------------------------------------------------------------------------
}
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason) //這個(gè)為緩存移除時(shí)的回調(diào)函數(shù),一定要保持與 Cache.Insert()方法中的最后一個(gè)參數(shù)名字一致,
//這里使用了委托,你可以在 Cache.Insert()這個(gè)函數(shù)中轉(zhuǎn)定義看到的,所以這里的格式就只能按我寫的這種方法簽名寫。
{
System.IO.File.WriteAllText(Server.MapPath("log.txt"),"緩存移除原因?yàn)?"+reason.ToString());
}
八、配置文件中的緩存設(shè)置
--------------------------------------------------------------------------------
我們服務(wù)器有開啟緩存功能, 緩存功能可以減少您訪問網(wǎng)站時(shí)候網(wǎng)站在服務(wù)器里面的編譯時(shí)間, 大大加快您網(wǎng)站的訪問速度, 如果您需要對(duì)您網(wǎng)站進(jìn)行頻繁更新的話, 您可以考慮暫時(shí)將緩存時(shí)間減少,或者暫時(shí)關(guān)閉緩存
請(qǐng)將下列代碼放進(jìn)web.config文件里面放在您網(wǎng)站的根目錄;
1.在web.config里面設(shè)置縮小緩存的時(shí)間,請(qǐng)?jiān)趙eb.config里面用下面的定義
system.webServer>
caching>
profiles>
remove extension=".aspx" />
add extension=".aspx" policy="CacheForTimePeriod"
kernelCachePolicy="DontCache" duration="00:00:01" varyByQueryString="*" />
/profiles>
/caching>
/system.webServer>
2. 如果要關(guān)閉某個(gè)頁面的caching功能,請(qǐng)?jiān)趙eb.config里面用下面的定義
configuration>
location path="showStockPrice.asp">
system.webServer>
caching>
profiles>
remove extension=".asp" />
add extension=".asp" policy="DontCache" kernelCachePolicy="DontCache"/>
/profiles>
/caching>
/system.webServer>
/location>
/configuration>
3. 如果要關(guān)閉整個(gè)程序的caching功能,請(qǐng)?jiān)趙eb.config里面用下面的定義
configuration>
system.webServer>
caching>
profiles>
remove extension=".asp" />
add extension=".asp" policy="DontCache" kernelCachePolicy="DontCache"/>
/profiles>
/caching>
/system.webServer>
/configuration>
4. 如果要關(guān)閉根目錄某個(gè)或某幾個(gè)文件夾的caching功能,請(qǐng)?jiān)趙eb.config里面用下面的定義
location path="~/folderA,~/folderB">
system.webServer>
caching>
profiles>
remove extension=".asp" />
add extension=".asp" policy="DontCache" kernelCachePolicy="DontCache"/>
/profiles>
/caching>
/system.webServer>
/location>
/configuration>
標(biāo)簽:周口 百色 臺(tái)州 朝陽 新鄉(xiāng) 朔州 洛陽 喀什
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《對(duì)asp.net緩存 的深入了解》,本文關(guān)鍵詞 對(duì),asp.net,緩存,的,深入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。