緩存的基本用法介紹:我推薦看下 asp.net緩存。
本篇,我主要寫(xiě)下一般sql的緩存依賴,還有使用Mvc過(guò)濾器的數(shù)據(jù)庫(kù)緩存依賴。
1.緩存:是把你要訪問(wèn)的資源,放在內(nèi)存中,占用一定的內(nèi)存空間,從而是用戶讀取內(nèi)存中的數(shù)據(jù),進(jìn)而減少讀取數(shù)據(jù)庫(kù),或資源文件的次數(shù),從而對(duì)你的程序并發(fā)量,以及返回請(qǐng)求速率上得到提高的一種機(jī)制。
2.緩存的不及時(shí)性:由于在緩存的作用時(shí)間內(nèi),數(shù)據(jù)放在內(nèi)存中,不知道數(shù)據(jù)源是否已經(jīng)改變,從而是信息失去即時(shí)效應(yīng)。
3.解決不及時(shí)性:為啦解決第二條的不及時(shí)性,微軟想到的就是緩存依賴
4.緩存依賴:就是緩存通過(guò)監(jiān)測(cè)依賴項(xiàng)(文件或數(shù)據(jù)庫(kù))的讀寫(xiě),來(lái)通知緩存是否過(guò)期的一種機(jī)制。比如,依賴項(xiàng)是123.txt文件,緩存的數(shù)據(jù)是234.txt中的數(shù)據(jù),那么緩存機(jī)制可通過(guò)監(jiān)測(cè)123.txt文件中數(shù)據(jù)的是否變化,來(lái)移除緩存234.txt文件的數(shù)據(jù)。感覺(jué)扯淡,還是上代碼更給力。
//文件緩存依賴 if (cache.Get("key") == null)//如果依賴項(xiàng)中的數(shù)據(jù)發(fā)生變化,此會(huì)被通知緩存清空(系統(tǒng)完成清空) { CacheDependency dp = new CacheDependency(Server.MapPath("/Data/123.txt"));//建立緩存依賴項(xiàng)dp string str = DoIOFile.ReadFiles("/Data/111.txt"); cache.Insert("key", str, dp); } Response.Write(cache.Get("key")); //如果123.txt這個(gè)文件的內(nèi)容不變就一直讀取緩存中的數(shù)據(jù),一旦123.txt文件中的數(shù)據(jù)改變里面重新讀取111.txt文件中的數(shù)據(jù)效果:緩存的數(shù)據(jù)是111.txt中的數(shù)據(jù),111.txt中的數(shù)據(jù)發(fā)生變化,鑰匙為key的緩存不會(huì)被清空,也就是依舊顯示沒(méi)改前的數(shù)據(jù)。但是如果緩存依賴項(xiàng)123.txt中的數(shù)據(jù)一旦發(fā)生變化,緩存立馬被清空,重新寫(xiě)入緩存中新的數(shù)據(jù)。這就是緩存依賴的好處,你可以試下,我不忽悠你。
//文件夾緩存依賴 if (cache.Get("key") == null)//如果依賴項(xiàng)中的數(shù)據(jù)發(fā)生變化,此會(huì)被通知緩存清空(系統(tǒng)完成清空) { CacheDependency dp = new CacheDependency(Server.MapPath("/Data"));//建立緩存依賴項(xiàng)dp string str = DoIOFile.ReadFiles("111.txt"); cache.Insert("key", str, dp); } Response.Write(cache.Get("key")); //如果123.txt這個(gè)文件的內(nèi)容不變就一直讀取緩存中的數(shù)據(jù),一旦123.txt文件中的數(shù)據(jù)改變里面重新讀取111.txt文件中的數(shù)據(jù)效果:這里/Data是個(gè)文件夾,他下面直屬Data所有一級(jí)文件(就是不能算嵌套文件夾的文件)如果有變動(dòng),都會(huì)觸發(fā)通知,清空緩存。
//多文件依賴項(xiàng) if (cache.Get("key") == null)//如果依賴項(xiàng)中的數(shù)據(jù)發(fā)生變化,此會(huì)被通知緩存清空(系統(tǒng)完成清空) { CacheDependency dp1 = new CacheDependency(Server.MapPath("/Data/123/123.txt")); //這里是監(jiān)視文件或目錄 CacheDependency dp2 = new CacheDependency(Server.MapPath("/Data/123.txt")); CacheDependency[] dps = new CacheDependency[] { dp1, dp2 }; AggregateCacheDependency aDp = new AggregateCacheDependency(); //多個(gè)依賴項(xiàng) aDp.Add(dps); string str = DoIOFile.ReadFiles("111.txt"); cache.Insert("key", str, aDp); } Response.Write(cache.Get("key")); 效果:依賴項(xiàng)中的任何一個(gè)文件有變動(dòng),緩存清空,寫(xiě)入新緩存。
mvc中緩存的使用方法相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,只用在過(guò)濾器上定義一下就行啦,其它的我就不累述啦,與webForm無(wú)異。
[OutputCache(Duration = 20)] //定義緩存,秒為單位,Duration是必填項(xiàng) public ActionResult Index() { string str = DoIOFile.ReadFiles("/111.txt"); Response.Write(str); return View(); }具體配置詳見(jiàn):http://msdn.microsoft.com/zh-cn/library/system.web.mvc.outputcacheattribute.aspx
這個(gè)多少有點(diǎn)繁瑣,跟著做。
1.打開(kāi)項(xiàng)目配置文件
<connectionStrings> <add name="Am_WeixinWeb" connectionString="data source=192.168.1.200;initial catalog=Am_WeixinWeb;uid=sa;passWord=lh1234;" /> </connectionStrings>
<system.web> <caching> <sqlCacheDependency enabled="true" pollTime="2000"> <databases> <add name="Test" connectionStringName="Am_WeixinWeb" /> </databases> </sqlCacheDependency> </caching>
注記:pollTime,毫秒為單位,意識(shí)是每隔2秒檢測(cè)下數(shù)據(jù)庫(kù),檢測(cè)表是否有發(fā)生變化。connectionStringName為數(shù)據(jù)庫(kù)鏈接字符串。
2.啟動(dòng)數(shù)據(jù)庫(kù)緩存依賴
在C盤(pán)中,搜索到工具aspnet_regsql.exe
在命令中 cd:運(yùn)行到此工具的文件下,鍵入下面命令
aspnet_regsql -C "data source=;initial catalog=codematic;user id=sa;password=" -ed -et -t "T_table"

參數(shù):-c 后跟連接字符串,-t后接建立緩存依賴的表名
工具命令參數(shù)列表詳見(jiàn):http://msdn.microsoft.com/zh-cn/library/ms229862
3.使用緩存依賴項(xiàng)
//sql緩存依賴 DataSet ds = new DataSet(); if (cache.Get("key") == null) { string conStr = Doxml.ReadWebConfigConnectionStrings("Am_WeixinWeb"); SqlConnection conn = new SqlConnection(conStr); string sql = "select top(1) recContent from Am_rec效果:數(shù)據(jù)庫(kù)Am_WeixinWeb中表Am_recProScheme中的數(shù)據(jù)有所變動(dòng),則清空緩存,重新寫(xiě)入。
Mvc過(guò)濾器中配置緩存依賴(數(shù)據(jù)庫(kù))
1.打開(kāi)項(xiàng)目配置文件
<connectionStrings> <add name="Am_WeixinWeb" connectionString="data source=192.168.1.200;initial catalog=Am_WeixinWeb;uid=sa;password=lh1234;" /> </connectionStrings>
<caching> <sqlCacheDependency enabled="true" pollTime="2000"> <databases> <add name="Test" connectionStringName="Am_WeixinWeb" /> </databases> </sqlCacheDependency> </caching>
注記:pollTime,毫秒為單位,意識(shí)是每隔2秒檢測(cè)下數(shù)據(jù)庫(kù),檢測(cè)表是否有發(fā)生變化。connectionStringName為數(shù)據(jù)庫(kù)鏈接字符串。
2.配置過(guò)濾器
//mvc緩存依賴 [OutputCache(Duration = 20, SqlDependency = "Test:Am_recProScheme")] //Test:為緩存配置的key,后面跟的是緩存依賴表 public ActionResult Index() { Response.Write(db.Am_recProScheme.FirstOrDefault().recContent); return View(); }效果:數(shù)據(jù)庫(kù)Am_WeixinWeb中表Am_recProScheme中的數(shù)據(jù)有所變動(dòng),則清空緩存,重新寫(xiě)入。
本文以實(shí)用簡(jiǎn)略為主,如有探討,可加左上方技術(shù)交流群,謝謝閱讀,愿能給你一點(diǎn)點(diǎn)幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注