Web Service Cache

Source: Internet
Author: User

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.

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.