Because Microsoft's Web service implementation is based on ASP.net, it makes sense to cache. there are two major cache types: Output cache and data cache. because the Cache Management of the web service is basically the same as that of Asp.net, let's not talk about its principle, but mainly about how it is implemented in the Web service.
Output Cache
The implementation method is to add the attribute cacheduration in the webmethod feature, as shown in the following code:
[Webmthod (cacheduration = 30)]
Public String helloworld (){...}
The Unit is seconds.
Data Cache
Data caching is mainly stored in context. cache, and there is a general design mode for data caching.
That is to create a private method to retrieve all the data (the relative data depends on the project) and cache the data. We recommend that you use the public webmethod methods, this method filters out relevant data from the private method and returns it to the client. the code snippet is as follows:
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] // note: the code here is just an example, and its syntax is problematic. It mainly indicates that the alluser object is filtered Based on the passed Department parameters and then returned.
}
Because Microsoft's Web service implementation is based on ASP.net, it makes sense to cache. there are two major cache types: Output cache and data cache. because the Cache Management of the web service is basically the same as that of Asp.net, let's not talk about its principle, but mainly about how it is implemented in the Web service.
Output Cache
The implementation method is to add the attribute cacheduration in the webmethod feature, as shown in the following code:
[Webmthod (cacheduration = 30)]
Public String helloworld (){...}
The Unit is seconds.
Data Cache
Data caching is mainly stored in context. cache, and there is a general design mode for data caching.
That is to create a private method to retrieve all the data (the relative data depends on the project) and cache the data. We recommend that you use the public webmethod methods, this method filters out relevant data from the private method and returns it to the client. the code snippet is as follows:
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] // note: the code here is just an example, and its syntax is problematic. It mainly indicates that the alluser object is filtered Based on the passed Department parameters and then returned.
}