Example: Caching Web services

The following is an example of building a set of cache policies for a simple Web services application. The application in this example stores stock quotes and has operations to read, update the price of, and buy a given stock symbol.

Example: SOAP message for a GetQuote operation

The first message sample contains a SOAP message for a GetQuote operation that requests a quote for IBM. This is a read-only operation that gets its data from the back-end and is very cacheable. In this example, the SOAP message is cached, and a timeout is placed on its entries to guarantee the quotes it returns are not too out of date.

POST /soap/servlet/soaprouter  
HTTP/1.1  
Host: www.myhost.com  
Content-Type: text/xml; charset="utf-8"  
SOAPAction: urn:stockquote-lookup
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:getQuote xmlns:m="urn:stockquote:>
<symbol>IBM</symbol>
</m:getQuote>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The SOAPAction HTTP header in the request is defined in the SOAP specification and is used by HTTP proxy servers to dispatch requests to particular HTTP servers. WAS dynamic cache can use this header in its cache policies to build IDs without having to parse the SOAP message.

Example: SOAP message for a BuyQuote operation

The second message sample illustrates a SOAP message for a BuyQuote operation. While the first message is cacheable, this message is not, because it updates the back-end database.

POST /soap/servlet/soaprouter 
HTTP/1.1  
Host: www.myhost.com 
Content-Type: text/xml; charset="utf-8" 
SOAPAction: urn:stockquote-update
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:buyStock xmlns:m="urn:stockquote:>
<symbol>IBM</symbol>
</m:getQuote>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The graphic illustrates how to invoke methods with the SOAP messages. In Web services terms, especially Web Service Definition Language (WSDL), a service is a collection of operations such as getQuote and buyStock. A body element namespace (urn:stockquote in our example) defines a service, and the name of the first body element indicates the operation.

Invoke methods with SOAPmessages:caching

The following is an example of WSDL for the getQuote operation:

<?xml version="1.0"?>
<definitions name="StockQuoteService-interface"
targetNamespace="http://www.getquote.com/StockQuoteService-interface"
xmlns:tns="http://www.getquote.com/StockQuoteService-interface"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns=soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
<message name="SymbolRequest">
<part name="return" type="xsd:string"/>
</message>
<portType name="StockQuoteService">
<operation name="getQuote">
<input message="tns:SymbolRequest"/>
<output message="tns:QuoteResponse"/>
</operation>
</portType>
<binding name="StockQuoteServiceBinding"
type="tns:StockQuoteService">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getQuote">
<soap:operation soapAction="urn:stockquote-lookup"/>
<input>
<soap:body use="encoded" namespace="urn:stockquote"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:stockquotes"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>>
</binding>
</definition>

To build a set of cache policies for a Web services application, configure WAS dynamic cache to recognize cacheable service operation of the operation.

WAS inspects the HTTP request to determine if an incoming message can cache, based on the cache policies defined for an application. In this example, stockquote-lookup is cached, and buyStock and stock-update are not cached. In the cachespec.xml file for this Web application, the cache policies need to be defined for these services, so the dynamic cache can handle both SOAPAction and service operation.

WAS uses the operation and the message body in Web services cache IDs, each of which has a component associated with them. Therefore, each Web services <cache-id> rule contains only two components. The first is for the operation. Because you can perform the stockquote-lookup operation by either using a SOAPAction header or a service operation in the body, define two different <cache-id> elements, one for each method. The second component is of type "body", and defines how WAS should incorporate the message body into the cache ID. Use a hash of the body, although it is legal to use the literal incoming message in the ID.

The incoming HTTP request is analyzed by WAS to determine which of the <cache-id> rules match. Then, the rules are applied to form cache or invalidation IDs.

The following is sample code of a cachespec.xml file that defines SOAPAction and servicesOperation rules:

<cache>
<cache-entry>
<class>webservice</class>
<name>/soap/servlet/soaprouter</name>
<sharing-policy>not-shared</sharing-policy>
<cache-id>
<component id="" type=SOAPAction>
<value>urn:stockquote-lookup</value>
</component>
<component id="Hash" type="SOAPEnvelope"/>
<timeout>3600</timeout>
<priority>1<priority>
</cache-id>
<cache-id>
<component id="" type="serviceOperation">
<value>urn:stockquote:getQuote</value>
</component>
<component id="Hash" type="SOAPEnvelope"/>
<timeout>3600</timeout>
<priority>1</priority>
</cache-id>
</cache-entry>
</cache>