[Java programming language only]

Configuring a Spring cache provider

Spring Framework Version 3.1 introduced a new cache abstraction. With this new abstraction, you can transparently add caching to an existing Spring application. You can use WebSphere® eXtreme Scale as the cache provider for the cache abstraction.

Before you begin

About this task

By using the cache abstraction in the Spring framework, you can reduce the number of times that your Spring application methods run. When configured, the results of a particular method are placed in the cache. When the method is run again, the abstraction checks the cache to see if the method results are already in the cache. If the results are in the cache, the results are returned from the cache and the method does not run again. Therefore, you can reduce the number of times that expensive methods run, also decreasing the average response time of your application.

Procedure

  1. Configure your container servers to use the configuration files for Spring.

    You must start container servers before the Spring application that accesses the cache starts. To start container servers, see Starting stand-alone servers that use the ORB transport.

    The default XML configuration files for starting a container server for the eXtreme Scale Spring cache provider are in one of the following locations:
    • Stand-alone installations: wxs_install_root/ObjectGrid/spring/etc
    • WebSphere Application Server installations: was_root/optionalLibraries/ObjectGrid/spring/etc
    The files are named spring-remote-objectgrid.xml and spring-remote-deployment.xml. You can use these files as-is, customize these files, or create your own configuration files.
    Run the following command to start a stand-alone container server for the eXtreme Scale Spring cache provider. Run the following command from the wxs_home/ObjectGrid/bin directory:
    [Windows]
    startOgServer.bat container1 -objectGridFile ../spring/etc/spring-remote-objectgrid.xml 
    -deploymentPolicyFile ../spring/etc/spring-remote-deployment.xml

    [Unix]
    startOgServer.sh container1 -objectGridFile ../spring/etc/spring-remote-objectgrid.xml 
    -deploymentPolicyFile ../spring/etc/spring-remote-deployment.xml

    [Windows][Version 8.6 and later]
    startXsServer.bat container1 -objectGridFile ../spring/etc/spring-remote-objectgrid.xml 
    -deploymentPolicyFile ../spring/etc/spring-remote-deployment.xml

    [Unix][Version 8.6 and later]
    startXsServer.sh container1 -objectGridFile ../spring/etc/spring-remote-objectgrid.xml 
    -deploymentPolicyFile ../spring/etc/spring-remote-deployment.xml
  2. Configure Spring Inversion of Control (IoC) container to use WebSphere eXtreme Scale as the cache provider. The WebSphere eXtreme Scale cache implementation resides under the com.ibm.websphere.objectgrid.spring package. Define the following beans in your Spring IoC container configuration.
    <bean id="wxsCSDomain" class="com.ibm.websphere.objectgrid.spring.ObjectGridCatalogServiceDomainBean"
      p:catalog-service-endpoints="CATALOG_SERVICE_ENDPOINTS" 
      p:client-override-xml="CLIENT_OVERRIDE_XML (optional)"
      p:client-security-config="CLIENT_SECURITY_CONFIG (optional)" />
    
    <bean id="wxsGridClient" class="com.ibm.websphere.objectgrid.spring.ObjectGridClientBean" 
      p:object-grid-name="OBJECT_GRID_NAME(optional)"
      p:catalog-service-domain-ref="wxsCSDomain" />
    
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
      <property name="caches">
        <set>   
          <bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache"
             p:name="CACHE_NAME"
             p:map-name="MAP_NAME (optional)"
    	        p:object-grid-client-ref="wxsGridClient" />
         </set>
       </property>
    </bean>
    CATALOG_SERVICE_ENDPOINTS
    Specifies the Object Request Broker (ORB) host and port number.
    CLIENT_OVERRIDE_XML (optional)
    Specifies an absolute or relative path to an ObjectGrid XML file on which to alter settings on the client side as a Spring resource. For information about specifying resources in Spring, see Spring Framework Reference Documentation: Resources.

    Example:p:client-override-xml="file:/path/to/objectgrid.xml"

    Example:p:client-override-xml="classpath:com/example/app/override-objectgrid.xml"

    Example:p:client-override-xml="http://myserver/override-objectgrid.xml"

    Example:p:client-override-xml="ftp://myserver/override-objectgrid.xml"

    CLIENT_SECURITY_CONFIG (optional)
    Specifies an absolute or relative path to a client.properties file as a Spring resource. For information about specifying resources in Spring, see Spring Framework Reference Documentation: Resources.

    Example: p:client-security-config="file:/path/to/client.properties"

    OBJECT_GRID_NAME (optional)
    Specifies the ObjectGrid name. This parameter is not needed if the container servers are started with the provided XML configuration files. This parameter must be consistent with the XML configuration files that are used to start the container servers.
    CACHE_NAME
    Specifies the name of the cache that is specified in your Spring caching application.
    MAP_NAME (optional)
    Specifies the name of the backing map for a cache. This parameter is not needed if the container servers are started with the provided XML configuration files. This parameter must be consistent with the XML configuration files that are used to start the container servers. If you use the provided XML configuration files, the MAP_NAME value is not needed. The maps for the data grid are created automatically when the Spring application runs. The dynamic map name starts with IBM_SPRING_PARTITIONED_. For example: IBM_SPRING_PARTITIONED_1, IBM_SPRING_PARTITIONED_2, and so on.

Example

The following snippet creates two caches, named default and books hosted by the catalog service domain at localhost:2809.
<bean id="wxsCSDomain" class="com.ibm.websphere.objectgrid.spring.ObjectGridCatalogServiceDomainBean" 
	p:catalog-service-endpoints ="localhost:2809" />  
<bean id="wxsGridClient" class="com.ibm.websphere.objectgrid.spring.ObjectGridClientBean"    
	p:catalog-service-domain-ref="wxsCSDomain" /> 
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
	<property name="caches">   
		<set>    
			<bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache" 
				p:name="default"     
				p:object-grid-client-ref="wxsGridClient" />   
			<bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache"     
				p:name="books"     
				p:object-grid-client-ref="wxsGridClient" />   
		</set>  
	</property>
</bean>