日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

webservice 缓存机制

發布時間:2024/6/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webservice 缓存机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文轉載: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 { //提供下載和分析功能的后臺代碼類,TestOP private 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

?


CacheDuration:設置響應應在緩存中保留的秒數。這樣Web Service就不需要重復執行多遍,可以提高訪問效率,而CacheDuration就是指定緩存時間的屬性。
示例代碼如下:
[WebService(Description="測試WebService屬性", Name="MyService", Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
[WebMethod( EnableSession=true, CacheDuration=10)]
public string GetCNDateTime()
{
return DateTime.Now.ToString("yyyy年MM月dd日hh時mm分ss秒ms毫秒");
}
}
上面的服務方法緩存了10秒鐘。
在我們運行該服務,手動調用方法的時候會發現好像緩存并沒有起作用,每次調用的顯示的時間總會變化。其實該緩存的效果在直接運行的時候并不會起作用,當我們編寫客戶端代碼調用該服務的時候會發現緩存的確是起作用了。
客戶端代碼如下:
public static void Main(string[] args)
{
Services.MyServiceSoapClient client = new Client.Services.MyServiceSoapClient();
string str = client.GetCNDateTime();
Console.WriteLine(str);
}
在運行該客戶端代碼的時候,在10秒鐘之內的兩次運行顯示的時間是不變的。

轉載于:https://www.cnblogs.com/51net/p/4117205.html

總結

以上是生活随笔為你收集整理的webservice 缓存机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。