Sunday, April 22, 2012

Caching SOAP Response of Web Service

The caching is capability to maintain an in memory store where data, objects and various items are stored for reuse. Caching improves the response time of an application there by increasing the performance of application.
   Web services also have caching capability; The CacheDuration   is an attribute of webmethod that let us to specify duration of time for which SOAP response is stored in memory

Public string GetCurrentDate()
      return DateTime.Now.ToString(“yyyy-MM-dd hh:mm:ss”);

Here CacheDuration attribute set to 50 seconds, the value of CacheDuration should always be an integer no fractional values are allowed.

Now when we call this webmethod first time the system’s current date time will be our response, this response is cached for 50 seconds, within 50 seconds of last request if another request comes then lastly cached response is provided but after 50 seconds this cached response get automatically discarded the caller will get recent system date time.
   Point to note is that cached response is dependent on set of input parameters i.e.
If your webmethod input parameter combination differ from last requested input parameter combination then even if last request was within cache duration you will get response from cache.

Public int AddInt(int num1,int num2)
      Return num1 + num2;

On first call of AddInt(10,20) will cache response but immediate next call to AddInt(20,10) will not be from cached data as both call differ in input parameter combination even if there time lag is within Cache duration of web method.

No comments:

Post a Comment