Web Service 缓存
由于微軟的Web service實(shí)現(xiàn)是基于Asp.net,所以會出現(xiàn)緩存也就在情理之中了.主要有2中緩存:輸出緩存和數(shù)據(jù)緩存.由于Web Service的緩存管理與asp.net基本是一樣的,就不說他的原理,主要說一下在Web service中是如何實(shí)現(xiàn)的.
輸出緩存
實(shí)現(xiàn)方法是在WebMethod這個(gè)特性里面加一個(gè)屬性CacheDuration,如下代碼所示
[WebMthod(CacheDuration=30)]
public string HelloWorld(){...}
其單位為秒.
數(shù)據(jù)緩存
數(shù)據(jù)緩存主要是存儲在Context.Cache之中.而關(guān)于數(shù)據(jù)緩存有一個(gè)比較通用的設(shè)計(jì)模式.
那就是建立一個(gè)private 方法,拿取所有的數(shù)據(jù)(相對所有的數(shù)據(jù),依據(jù)項(xiàng)目情況而定),并把數(shù)據(jù)緩存起來,在建議各public的WebMethod方法,此方法就從私有的方法過濾出相關(guān)的數(shù)據(jù)返回給客戶端.代碼片段如下
private DataSet GetAllUsers()
{
DataSet ds =new DataSet();
if(Context.Cache["cachename"] !=null)
{
return (DataSet)Context.Cache["cachename"];
}
else
{
//....some code to retrieve data from some store
//cache
Context.Cache.Insert("cachename",ds,null,DateTime.Now.AddMinutes(10),TimeSpan.Zero);
return ds;
}
}
//retrieve all users that belongs to a department.
[WebMethod]
public User[] GetUser(string DeptID)
{
DataSet alluser = GetAllUsers();
User[] users =alluser.Tables[0].Rows[5] //注:此處代碼僅僅是示例,本身語法是有問題,主要是說明在alluser對象中依據(jù)傳過來的部門參數(shù)進(jìn)行過濾后,然后返回.
}
轉(zhuǎn)載于:https://www.cnblogs.com/scgw/archive/2011/03/18/1988104.html
總結(jié)
以上是生活随笔為你收集整理的Web Service 缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 瑶柱多少钱一斤啊?
- 下一篇: 试管婴儿怎么取精子?