緩存的基本用法介紹:我推薦看下 asp.net緩存。
本篇,我主要寫下一般sql的緩存依賴,還有使用Mvc過濾器的數據庫緩存依賴。
1.緩存:是把你要訪問的資源,放在內存中,占用一定的內存空間,從而是用戶讀取內存中的數據,進而減少讀取數據庫,或資源文件的次數,從而對你的程序并發量,以及返回請求速率上得到提高的一種機制。
2.緩存的不及時性:由于在緩存的作用時間內,數據放在內存中,不知道數據源是否已經改變,從而是信息失去即時效應。
3.解決不及時性:為啦解決第二條的不及時性,微軟想到的就是緩存依賴
4.緩存依賴:就是緩存通過監測依賴項(文件或數據庫)的讀寫,來通知緩存是否過期的一種機制。比如,依賴項是123.txt文件,緩存的數據是234.txt中的數據,那么緩存機制可通過監測123.txt文件中數據的是否變化,來移除緩存234.txt文件的數據。感覺扯淡,還是上代碼更給力。
//文件緩存依賴 if (cache.Get("key") == null)//如果依賴項中的數據發生變化,此會被通知緩存清空(系統完成清空) { CacheDependency dp = new CacheDependency(Server.MapPath("/Data/123.txt"));//建立緩存依賴項dp string str = DoIOFile.ReadFiles("/Data/111.txt"); cache.Insert("key", str, dp); } Response.Write(cache.Get("key")); //如果123.txt這個文件的內容不變就一直讀取緩存中的數據,一旦123.txt文件中的數據改變里面重新讀取111.txt文件中的數據
效果:緩存的數據是111.txt中的數據,111.txt中的數據發生變化,鑰匙為key的緩存不會被清空,也就是依舊顯示沒改前的數據。但是如果緩存依賴項123.txt中的數據一旦發生變化,緩存立馬被清空,重新寫入緩存中新的數據。這就是緩存依賴的好處,你可以試下,我不忽悠你。
//文件夾緩存依賴 if (cache.Get("key") == null)//如果依賴項中的數據發生變化,此會被通知緩存清空(系統完成清空) { CacheDependency dp = new CacheDependency(Server.MapPath("/Data"));//建立緩存依賴項dp string str = DoIOFile.ReadFiles("111.txt"); cache.Insert("key", str, dp); } Response.Write(cache.Get("key")); //如果123.txt這個文件的內容不變就一直讀取緩存中的數據,一旦123.txt文件中的數據改變里面重新讀取111.txt文件中的數據
效果:這里/Data是個文件夾,他下面直屬Data所有一級文件(就是不能算嵌套文件夾的文件)如果有變動,都會觸發通知,清空緩存。
//多文件依賴項 if (cache.Get("key") == null)//如果依賴項中的數據發生變化,此會被通知緩存清空(系統完成清空) { CacheDependency dp1 = new CacheDependency(Server.MapPath("/Data/123/123.txt")); //這里是監視文件或目錄 CacheDependency dp2 = new CacheDependency(Server.MapPath("/Data/123.txt")); CacheDependency[] dps = new CacheDependency[] { dp1, dp2 }; AggregateCacheDependency aDp = new AggregateCacheDependency(); //多個依賴項 aDp.Add(dps); string str = DoIOFile.ReadFiles("111.txt"); cache.Insert("key", str, aDp); } Response.Write(cache.Get("key"));
效果:依賴項中的任何一個文件有變動,緩存清空,寫入新緩存。
mvc中緩存的使用方法相對來說比較簡單,只用在過濾器上定義一下就行啦,其它的我就不累述啦,與webForm無異。
[OutputCache(Duration = 20)] //定義緩存,秒為單位,Duration是必填項 public ActionResult Index() { string str = DoIOFile.ReadFiles("/111.txt"); Response.Write(str); return View(); }
具體配置詳見:http://msdn.microsoft.com/zh-cn/library/system.web.mvc.outputcacheattribute.aspx
這個多少有點繁瑣,跟著做。
1.打開項目配置文件
<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,毫秒為單位,意識是每隔2秒檢測下數據庫,檢測表是否有發生變化。connectionStringName為數據庫鏈接字符串。
2.啟動數據庫緩存依賴
在C盤中,搜索到工具aspnet_regsql.exe
在命令中 cd:運行到此工具的文件下,鍵入下面命令
aspnet_regsql -C "data source=;initial catalog=codematic;user id=sa;password=" -ed -et -t "T_table"
參數:-c 后跟連接字符串,-t后接建立緩存依賴的表名
工具命令參數列表詳見:http://msdn.microsoft.com/zh-cn/library/ms229862
3.使用緩存依賴項
//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效果:數據庫Am_WeixinWeb中表Am_recProScheme中的數據有所變動,則清空緩存,重新寫入。
Mvc過濾器中配置緩存依賴(數據庫)
1.打開項目配置文件
<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,毫秒為單位,意識是每隔2秒檢測下數據庫,檢測表是否有發生變化。connectionStringName為數據庫鏈接字符串。
2.配置過濾器
//mvc緩存依賴 [OutputCache(Duration = 20, SqlDependency = "Test:Am_recProScheme")] //Test:為緩存配置的key,后面跟的是緩存依賴表 public ActionResult Index() { Response.Write(db.Am_recProScheme.FirstOrDefault().recContent); return View(); }效果:數據庫Am_WeixinWeb中表Am_recProScheme中的數據有所變動,則清空緩存,重新寫入。
本文以實用簡略為主,如有探討,可加左上方技術交流群,謝謝閱讀,愿能給你一點點幫助。
新聞熱點
疑難解答