application與cache

來源:互聯網
上載者:User

標籤:io   ar   使用   sp   檔案   on   資料   bs   ad   

每個項目都有一些全域,常用的資訊,而這些資訊如果在每次使用時都載入,那必將耗費很大的資源,特別是對訪問壓力大的系統。因此,這個情況中,把這些全域資訊放到緩衝中是很必要的,放在緩衝中可以使得資料能夠很快的被讀取,節省了很多寶貴的CPU和IO。
項目中通常是用application 和cache來實現緩衝的功能。他們的用法分別為:

1)application:
application["test"] = "this is a application message!";

2)cache

 Cache.Add("Key1", "Value");

兩種用法都很相似,都是採用名/值對的方式來儲存資料,而在讀取資料時也只要用 鍵 就可以擷取緩衝的值。

而2種相比,到底哪種更有優勢呢? 答案是CACHE在使用上更具有靈活性。特點如下:

1。自有的按時更新緩衝的機制

有的項目需要定時擷取最新資料的需求,如天氣預報,可能間隔10分鐘 就要讀取一次需求,那這可以利用CACHE本身的方法來實現。


//監視某個時間
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    DateTime dt = DateTime.Now.AddSeconds(10);

    // Create a cache entry.
    Cache["key1"] = "Value 1";
    CacheDependency dependency = new CacheDependency(null,  dt);

    Cache.Insert("key2", "Value 2", dependency);

    DisplayValues();
}






2.當緩衝的源修改時,可以重新更新緩衝。這個緩衝源可以是變數,也可以是檔案,或者目錄。

//監視某個變數
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    //DateTime dt = DateTime.Now.AddSeconds(10);

    // Create a cache entry.
    Cache["key1"] = "Value 1";

    // Make key2 dependent on key1.
    String[] dependencyKey = new String;
    dependencyKey[0] = "key1";
    CacheDependency dependency = new CacheDependency(null, dependencyKey, null);

    Cache.Insert("key2", "Value 2", dependency);

    DisplayValues();
}





3.同時以多種搭配來自動更新緩衝,如同時監視某個檔案,並且在指定間隔的時間內自動更新。

//監視某個時間和變數
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    DateTime dt = DateTime.Now.AddSeconds(10);

    // Create a cache entry.
    Cache["key1"] = "Value 1";

    // Make key2 dependent on key1.
    String[] dependencyKey = new String;
    dependencyKey[0] = "key1";
    CacheDependency dependency = new CacheDependency(null, dependencyKey, dt);

    Cache.Insert("key2", "Value 2", dependency);

    DisplayValues();
}





比起APPLICATION來,CACHE更顯得靈活。

application與cache

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.