Caching Web Services


A cache on a system is an in-memory store where data, objects, and various items are stored for reuse. Many applications and Web sites use caching today in order to increase performance. How is this done? Take a look at the browser as a prime example of a device that uses caching to greatly increase performance.

When you pull up a page in your browser for the first time, the browser takes the items from the page (most notably the images) and stores these objects in memory. The next time you return to the same page, the browser uses the in-memory images and data to generate the page (if no changes were made to the original page). Using the in-memory version of the objects to generate the page greatly enhances performance.

Using an in-memory store is also an easy way to greatly increase the performance of your Web services with very little work on your end. Looking at .NET-based Web services, you can use a feature that allows you to use an output caching capability. Output caching is a capability to store responses that are generated from your Web service to use for subsequent requests.

Output caching can be controlled in dealing with Web services on the WebMethod level by using the CacheDuration property. This is illustrated in Listing 19-13.

Listing 19-13: Caching using the CacheDuration property

image from book
      [WebMethod(CacheDuration=45)]      public DataSet GetCustomers()      {         SqlConnection conn;         SqlDataAdapter myDataAdapter;         DataSet myDataSet;         string cmdString = "Select * From Customers";         conn = new SqlConnection("Server=localhost;uid=sa;pwd=;database=Northwind");         myDataAdapter = new SqlDataAdapter(cmdString, conn);         myDataSet = new DataSet();         myDataAdapter.Fill(myDataSet, "Customers");         return myDataSet;      } 
image from book

The value provided through the CacheDuration property is a number that represents the number of seconds that ASP.NET stores the response from the Web service in memory. In this example, the WebMethod GetCustomers() returns a large result set of customers that most probably doesn't change very often; therefore, it is a prime candidate for caching. This dataset is cached by ASP.NET for 45 seconds. After 45 seconds, the cache is destroyed and the response is generated again (and stored again).

It is important to understand caching behaviors. If your WebMethod requires input parameters, then the caching occurs for each unique set of data based upon the parameters input into the system. For instance, if a consumer of a WebMethod that requires a single parameter invokes the WebMethod using a single parameter value of A to get a response, this response is cached according to the instructions provided through the CacheDuration property. Then, if a second consumer of the WebMethod uses a parameter value of B to get his response, this response is also cached along with the result that came from using a parameter value of A. At this point, two cached results exist in memory: one with the result set from a parameter value of A and another with the result set from the parameter value of B. If a third consumer uses either A or B and the cache has not expired for either of these result sets, the cached copy of the response is used.

Understanding this process is important. If your WebMethod can return a wide variety of result sets to the consumer from all the possible parameter values that can be provided by the end user, caching might not be that beneficial. Use caching intelligently.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net