WebService的緩存

 

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;
}
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章