asp.net的狀態管理與cache

來源:互聯網
上載者:User
在我的印象中,談到狀態管理,更多的是談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.cnblogs.com/aspnet2008/archive/2008/10/09/1307370.html
ASP.NET Cache緩衝的使用
http://www.cnblogs.com/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.cnblogs.com/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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.