webservice 緩衝機制

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

本文轉載:http://blog.csdn.net/zhdd1234/article/details/4555472

WebService的緩衝分為兩種,一種是簡單的輸出緩衝,一種是強大的資料緩衝

一、輸出緩衝
輸出緩衝的使用非常簡單,比較適用於WebService的參數比較少,結果比較單一的情況,例如股票資訊,可以設定5-10秒的緩衝,天氣預報,則可以設定30分鐘甚至數小時的緩衝

使用方法是:
在WebMethod屬性上指定CacheDuration屬性即可,例如



這樣,600秒內這個WebService的所有輸出資料都將從緩衝中讀取,不會真正做資料處理,如果事務代碼是訪問資料庫的話,現在這種方法就會比每次都訪問資料庫快得多。這種緩衝適合初接觸WebService的新手使用。

[WebMethod(Description = “Test”,CacheDuration=600)]
public string Test()
{
return “Test”;
}

 

 

要注意的是,不是所有服務都適合使用這種緩衝,例如每次結果都不一樣的,訪問數極高的服務,緩衝將會變得非常大,佔用很多伺服器的記憶體,卻沒有實際效果。

二、資料緩衝
想將你的WebService某些運行資料儲存起來?如果不使用本地的資料庫或者檔案,那麼緩衝是最好的選擇。這種緩衝不同於上面提到的輸出緩衝,它需要編寫代碼來實現,但是相對應的,它的功能非常強大,可以存放任何類型的資訊,並且你可以在任何時候檢索它。

 

雖然也可以使用Application來類比緩衝,但是這需要你自己管理記憶體釋放、使用者並發問題,在.net時代已經被拋棄,WebService下的緩衝使用Cache這個集合

 

using System.Web.Caching;
[WebMethod(Description = “Test”)]
public string Test()
{
string Content = “just4test”;

 

//建立資料緩衝
Context.Cache.Insert(”Test”, Content, null, DateTime.MaxValue,TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

string result = Context.Cache[”Test”].ToString();
return result;
}
}

 

在這裡,我們使用了Context.Cache屬性,Context.Cache.Insert方法用於將資料加入緩衝。這個方法一共有4種重載,在這個例子中,我們使用的是功能最全面的重載版本,我們以此為例:每一個參數分別是鍵名(使用方法類似於Session),值,依賴性,絕對到期時間,可變到期時間,緩衝優先順序,快取項目刪除時的委託方法絕對到期時間是固定的,DataTime.MaxValue在這裡表示永不到期;可變到期時間是一定時間內該緩衝沒有使用則自動失效,此處TimeSpan.Zero表示不使用可變到期。注意兩者只能設定一項,如果要使用可變到期,絕對到期必須是DataTime.MaxValue,例如

Context.Cache.Insert("Test", Content, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));

緩衝優先順序是Web伺服器清理它的可能性,在此的CacheItemPriority.NotRemovable表示通常不從緩衝中刪除,可以理解為永久性緩衝

通過依賴性,可以監視某個檔案或者其他緩衝的改動,如果有變化,則此緩衝失效,這非常有實用價值。例如:

CacheDependency de = new CacheDependency(Server.MapPath("1.xml"));
Context.Cache.Insert("Test", Content, de, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

這樣,1.xml檔案被刪除或者更改的時候,緩衝就會失效

三、實用舉例
實際使用中,我使用這樣一段代碼,當遠端某個檔案更新時,必須下載到本地,而緩衝負責儲存該檔案的檔案名稱和修改時間,每次用戶端請求檔案名稱和時間的時候,直接從緩衝讀取。每次定時下載程式(另有代碼)啟動的時候,getFiles()方法先檢查是否有新檔案(與本機快取比對),然後決定是否下載。

using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml;using System.Xml.Serialization;using TestOp;using System.Web.Caching;[WebService(Namespace = "Test")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class Test: System.Web.Services.WebService{//提供下載和分析功能的後台代碼類,TestOPprivate TestOp testOp;public Test(){testOp = newTestOp();}[WebMethod(Description = "下載檔案")]public string getFiles(){if (Context.Cache["FileName"] != null){string FN=Context.Cache["FileName"].ToString();testOp.GetHTML();testOp.GetMatch();if (FN.CompareTo(testOp.CheckFileName()) != 0){try{testOp.Download();Context.Cache.Insert("Time", testOp.CheckTime(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);Context.Cache.Insert("FileName", testOp.CheckFileName(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);return "ok";}catch{return "Ex";}}else{return "ok";}}else{try{testOp.GetHTML();testOp.GetMatch();testOp.Download();Context.Cache.Insert("Time", testOp.CheckTime(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);Context.Cache.Insert("FileName", testOp.CheckFileName(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);return "ok";}catch{return "Ex";}}}[WebMethod(Description = "檢查最新檔案時間")]public string CheckTime(){if (Context.Cache["Time"] ==null){try{this.getFiles();string result = Context.Cache["Time"].ToString();DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);return result;}catch{return "Ex";}}else{string result = Context.Cache["Time"].ToString();return result;}}[WebMethod(Description = "檢查最新檔案名稱")]public string CheckFileName(){if (Context.Cache["FileName"] == null){try{this.getFiles();string result = Context.Cache["FileName"].ToString();return result;}catch{return "Ex";}}else{string result = Context.Cache["FileName"].ToString();return result;}}}
View Code

 

webservice 緩衝機制

聯繫我們

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