asp.net的狀態管理與cache
在我的印象中,談到狀態管理,更多的是談application,session之類的,很少有談cache的,當然cache不屬于狀態管理的范圍。但是最近在工作中用到了cache,發現實際上Cache比其它的對象更易用,更實用
還是先把原先經常談到的對象羅列一次吧
1.服務器端
application
屬于全局控制,使用前要lock
session
每個用戶有自己的一個副本,有過期時間,不過過期時間不好控制
2.客戶端
cookie
每個用戶都有cookie,過期時間好設置,但有大小限制,一般為4K,瀏覽器可能不支持或禁用cookie
viewstate
每個頁面都有viewstate
hiddlefield
屬于控件,自由控制
下面來談cache。一般有三種分類:頁面級輸出緩存,頁片段緩存,數據緩存(cache對象),這里談的是cache對象。感覺cache,就是讓application和cookie來了個綜合,在服務器的內存中開辟了一塊空間來存放數據,任務人可以訪問,且可以靈活的設置過期時間,甚至可以設置過期時所引發的事件。
cache的用法與其它對象差不多,有add和insert兩種方法添加。
Inser方法可以使用可選參數,即使用默認參數,來實現緩存的添加。
Add()方法只能添加緩存中沒有的項,如果添加緩存中已有的項將失敗(但不會拋出異常),而Insert()方法能覆蓋原來的項。
和Application不同,不需要使用在插入緩存的時候進行鎖操作,Cache會自己處理并發。
Cache.Add(
KeyName,//緩存名
KeyValue,//要緩存的對象
Dependencies,//依賴項
AbsoluteExpiration,//絕對過期時間
SlidingExpiration,//相對過期時間
Priority,//優先級
CacheItemRemovedCallback);//緩存過期引發事件
用cache,主要就是要用它的依賴與過期,不然就和application沒區別了,依賴有三種:
文件依賴與其它依賴算一類,要用到CacheDependency對象
文件依賴:
CacheDependency fileDepends = new CacheDependency(Server.MapPath("Northwind.xml"));
Cache.Insert("GridViewDataSet", dsGrid, fileDepends);
其它依賴:
string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
CacheDependency cacheDepends = new CacheDependency(null, cacheDependsArray);
Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
一起用:
string[] fileDependsArray = {Server.MapPath("Northwind.xml")};
string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
CacheDependency cacheDepends = new CacheDependency(fileDependsArray, cacheDependsArray);
Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
還有一種依賴就是過期時間
過期時間有兩個參數:AbsoluteExpiration與SlidingExpiration。
AbsoluteExpiration可以設置緩存的絕對過期時間,如:
Cache.Insert("GridViewDataSet ", dsGrid, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration);
緩存會在添加起30分鐘后過期。
NoSlidingExpiration可以設置相對過期時間,如果緩存在NoSlidingExpiration設定的時間內沒有被訪問,緩存過期,如果在這段時間內有訪問,則緩存過期時間將會重置為原始值,如NoSlidingExpiration=20
在20分鐘內如果沒有被訪問,緩存過期,如果每次19分鐘訪問緩存,緩存將永遠不會過期。
Cache.Insert("DataGridDataSet", dsGrid, null,Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30));
有以下幾點需要注意:
1.Cache.NoAbsoluteExpiration是枚舉,表示無絕對過期時間
Cache.NoSlidingExpiration表示無相對過期時間
2.如果既設置了絕對過期時間又設置了相對過期時間,則參考以下:
如果 slidingExpiration 參數設置為 NoSlidingExpiration,則禁用可調整過期。如果將 slidingExpiration 參數設置為大于 Zero,則 absoluteExpiration 參數設置為 Now 加 slidingExpiration 參數中包含的值。如果在 absoluteExpiration 參數指定的時間之前從緩存請求該項,該項將再次放入緩存,并且 absoluteExpiration 將再次設置為 DateTime.Now 加 slidingExpiration 參數中包含的值。如果在 absoluteExpiration 參數中的日期以前并未從緩存中請求該項,則從緩存移除該項。
優先級
就是當服務器內存資源不足的時候,對緩存區的數據進行如何處理
NotRemovable: Items with this priority will not be evicted.
High: Items with this priority level are the least likely to be evicted.
AboveNormal: Items with this priority level are less likely to be evicted than items assigned Normal priority.
Default: This is equivalent to Normal.
Normal: The default value.
BelowNormal: Items with this priority level are more likely to be evicted than items assigned Normal priority.
Low: Items with this priority level are the most likely to be evicted.
雖然是英文,但很好理解,就是設置的越高,越不易被清除。
緩存失效事件處理
這需要一個委托:CacheDependency,當然,你直接把方法名寫在參數處也可以。
當緩存失效失效時就會激發這個事件,調用這個方法,這個方法有三個參數:
string str, object sender, CacheItemRemovedReason reason
用法:
public static CacheItemRemovedCallback onCallBack = null;
protected void Page_Load(object sender, EventArgs e)
{
CacheDependency dep = new CacheDependency(null, new string[] { "1", "2" });
onCallBack = CallBack;
Cache.Insert("A", "a", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5), CacheItemPriority.Default, onCallBack);
}
private void CallBack(string str, object sender, CacheItemRemovedReason reason)
{
System.IO.File.WriteAllText(@"c:\a.txt", reason.ToString());
}
注意:
我一開始想在過期觸發事件時給前臺一個提示,想法很好,但在實際中是不能實現的,因為Web訪問是無狀態的,它不可能知道這是誰設置的,而且它是公用的,難道要給每一個在線的人都發一個提示?!它只能在服務端做一些操作,比如寫文件,數據庫的訪問等。
由以上看來,它既能加快程序響應速度,提高性能,又是存在服務端,解決安全性問題,還能靈活設置依賴條件,真是用五個字來形容也不為過啊:很好很強大!
但是也不要濫用了,必竟是要占用服務器資源的啦!
實際運用:
我們一般都是在Web上展示數據庫中的數據,所以如果cache能與數據庫關聯就好了,但好象上面沒有提到數據庫啊!所以我們得換一個思路
建立一個觸發器,當數據庫中發生改變的時候,觸發觸發器寫文件,然后將Cache關聯到這個文件上,就可以實現數據庫的更改影響cache的消亡與生成了
以下是我參考的文檔,寫的比我好的多:
在asp.net中如何管理cache
http://www.rzrgm.cn/aspnet2008/archive/2008/10/09/1307370.html
ASP.NET Cache緩存的使用
http://www.rzrgm.cn/beyondjay/archive/2009/01/15/1376454.html
ASP.NET中CACHE的INSERT有兩個參數不理解
http://zhidao.baidu.com/question/9967841.html
asp.net cache用法,單點登陸
http://heisetoufa.javaeye.com/blog/315447
ASP.NET Cache 方案
http://www.rzrgm.cn/jeff377/archive/2008/08/28/1278989.html
ASP.NET中緩存Cache的使用心得
http://www.xueit.com/html/2009-02/21_702_00.html
利用Cache技術,來有效的提高ASP.NET網站性能
http://tech.it168.com/d/2008-01-14/200801142357414_1.shtml
詳情ASP.NET狀態管理緩存Cache應用
http://www.xueit.com/html/2009-02/21_703_00.html
還是先把原先經常談到的對象羅列一次吧
1.服務器端
application
屬于全局控制,使用前要lock
session
每個用戶有自己的一個副本,有過期時間,不過過期時間不好控制
2.客戶端
cookie
每個用戶都有cookie,過期時間好設置,但有大小限制,一般為4K,瀏覽器可能不支持或禁用cookie
viewstate
每個頁面都有viewstate
hiddlefield
屬于控件,自由控制
下面來談cache。一般有三種分類:頁面級輸出緩存,頁片段緩存,數據緩存(cache對象),這里談的是cache對象。感覺cache,就是讓application和cookie來了個綜合,在服務器的內存中開辟了一塊空間來存放數據,任務人可以訪問,且可以靈活的設置過期時間,甚至可以設置過期時所引發的事件。
cache的用法與其它對象差不多,有add和insert兩種方法添加。
Inser方法可以使用可選參數,即使用默認參數,來實現緩存的添加。
Add()方法只能添加緩存中沒有的項,如果添加緩存中已有的項將失敗(但不會拋出異常),而Insert()方法能覆蓋原來的項。
和Application不同,不需要使用在插入緩存的時候進行鎖操作,Cache會自己處理并發。
Cache.Add(
KeyName,//緩存名
KeyValue,//要緩存的對象
Dependencies,//依賴項
AbsoluteExpiration,//絕對過期時間
SlidingExpiration,//相對過期時間
Priority,//優先級
CacheItemRemovedCallback);//緩存過期引發事件
用cache,主要就是要用它的依賴與過期,不然就和application沒區別了,依賴有三種:
文件依賴與其它依賴算一類,要用到CacheDependency對象
文件依賴:
CacheDependency fileDepends = new CacheDependency(Server.MapPath("Northwind.xml"));
Cache.Insert("GridViewDataSet", dsGrid, fileDepends);
其它依賴:
string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
CacheDependency cacheDepends = new CacheDependency(null, cacheDependsArray);
Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
一起用:
string[] fileDependsArray = {Server.MapPath("Northwind.xml")};
string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
CacheDependency cacheDepends = new CacheDependency(fileDependsArray, cacheDependsArray);
Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
還有一種依賴就是過期時間
過期時間有兩個參數:AbsoluteExpiration與SlidingExpiration。
AbsoluteExpiration可以設置緩存的絕對過期時間,如:
Cache.Insert("GridViewDataSet ", dsGrid, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration);
緩存會在添加起30分鐘后過期。
NoSlidingExpiration可以設置相對過期時間,如果緩存在NoSlidingExpiration設定的時間內沒有被訪問,緩存過期,如果在這段時間內有訪問,則緩存過期時間將會重置為原始值,如NoSlidingExpiration=20
在20分鐘內如果沒有被訪問,緩存過期,如果每次19分鐘訪問緩存,緩存將永遠不會過期。
Cache.Insert("DataGridDataSet", dsGrid, null,Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30));
有以下幾點需要注意:
1.Cache.NoAbsoluteExpiration是枚舉,表示無絕對過期時間
Cache.NoSlidingExpiration表示無相對過期時間
2.如果既設置了絕對過期時間又設置了相對過期時間,則參考以下:
如果 slidingExpiration 參數設置為 NoSlidingExpiration,則禁用可調整過期。如果將 slidingExpiration 參數設置為大于 Zero,則 absoluteExpiration 參數設置為 Now 加 slidingExpiration 參數中包含的值。如果在 absoluteExpiration 參數指定的時間之前從緩存請求該項,該項將再次放入緩存,并且 absoluteExpiration 將再次設置為 DateTime.Now 加 slidingExpiration 參數中包含的值。如果在 absoluteExpiration 參數中的日期以前并未從緩存中請求該項,則從緩存移除該項。
優先級
就是當服務器內存資源不足的時候,對緩存區的數據進行如何處理
NotRemovable: Items with this priority will not be evicted.
High: Items with this priority level are the least likely to be evicted.
AboveNormal: Items with this priority level are less likely to be evicted than items assigned Normal priority.
Default: This is equivalent to Normal.
Normal: The default value.
BelowNormal: Items with this priority level are more likely to be evicted than items assigned Normal priority.
Low: Items with this priority level are the most likely to be evicted.
雖然是英文,但很好理解,就是設置的越高,越不易被清除。
緩存失效事件處理
這需要一個委托:CacheDependency,當然,你直接把方法名寫在參數處也可以。
當緩存失效失效時就會激發這個事件,調用這個方法,這個方法有三個參數:
string str, object sender, CacheItemRemovedReason reason
用法:
public static CacheItemRemovedCallback onCallBack = null;
protected void Page_Load(object sender, EventArgs e)
{
CacheDependency dep = new CacheDependency(null, new string[] { "1", "2" });
onCallBack = CallBack;
Cache.Insert("A", "a", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5), CacheItemPriority.Default, onCallBack);
}
private void CallBack(string str, object sender, CacheItemRemovedReason reason)
{
System.IO.File.WriteAllText(@"c:\a.txt", reason.ToString());
}
注意:
我一開始想在過期觸發事件時給前臺一個提示,想法很好,但在實際中是不能實現的,因為Web訪問是無狀態的,它不可能知道這是誰設置的,而且它是公用的,難道要給每一個在線的人都發一個提示?!它只能在服務端做一些操作,比如寫文件,數據庫的訪問等。
由以上看來,它既能加快程序響應速度,提高性能,又是存在服務端,解決安全性問題,還能靈活設置依賴條件,真是用五個字來形容也不為過啊:很好很強大!
但是也不要濫用了,必竟是要占用服務器資源的啦!
實際運用:
我們一般都是在Web上展示數據庫中的數據,所以如果cache能與數據庫關聯就好了,但好象上面沒有提到數據庫啊!所以我們得換一個思路
建立一個觸發器,當數據庫中發生改變的時候,觸發觸發器寫文件,然后將Cache關聯到這個文件上,就可以實現數據庫的更改影響cache的消亡與生成了
以下是我參考的文檔,寫的比我好的多:
在asp.net中如何管理cache
http://www.rzrgm.cn/aspnet2008/archive/2008/10/09/1307370.html
ASP.NET Cache緩存的使用
http://www.rzrgm.cn/beyondjay/archive/2009/01/15/1376454.html
ASP.NET中CACHE的INSERT有兩個參數不理解
http://zhidao.baidu.com/question/9967841.html
asp.net cache用法,單點登陸
http://heisetoufa.javaeye.com/blog/315447
ASP.NET Cache 方案
http://www.rzrgm.cn/jeff377/archive/2008/08/28/1278989.html
ASP.NET中緩存Cache的使用心得
http://www.xueit.com/html/2009-02/21_702_00.html
利用Cache技術,來有效的提高ASP.NET網站性能
http://tech.it168.com/d/2008-01-14/200801142357414_1.shtml
詳情ASP.NET狀態管理緩存Cache應用
http://www.xueit.com/html/2009-02/21_703_00.html

浙公網安備 33010602011771號