Alachisoft.com

Spring Cache

Spring is a popular Java framework that lets you simplify your application development and plug-in reusable pieces in your application. Many Java applications using Spring are also high transaction in nature and need scalability. As a result, Java Spring Cache framework lets you plug-in third party In-Memory Data Grid to meet your scalability requirements.

Spring introduced a concept of method-caching in Java using a project named Spring Cache. It's a simple way to cache method results to improve overall performance. This helps reducing the number of executions for a given set of parameters saving precious CPU load or expensive database trips. Spring Cache framework allows all of this with minimal code change.

Spring cache framework has a flexible architecture that allows third party In-Memory Data Grids to plug-in without any code changes to your application. TayzGrid is one such In-Memory Data Grid and is extremely fast and scalable.

TayzGrid provides integration with Spring Cache to let you store your POJO in the data grid and boost your application performance and scalability.

Live Webinars Spring Cache Documentation

Configure TayzGrid as Spring Cache


@Bean
public CacheManager cacheManager() {

    TayzGridConfigurationManager tgconfig = new TayzGridConfigurationManager();
    tgconfig.setConfigFile("./tayzgrid-spring.xml");
    TayzGridCacheManager cacheManager = new TayzGridCacheManager();
    cacheManager.setTayzGridConfigurationManager(tgconfig);
    return cacheManager;
}

Configuration Options for Java Spring Caching

And, below in tayzgrid-spring.xml where you can specify TayzGrid configuration for Spring Caching.


<application-config enable-cache-exception="true"
                    default-cache-name="DefaultCache">
  <caches>
    <cache name="CustomerCollectionCache"
           tayzgrid-instance="mycache"
           priority="normal"
           expiration-type="absolute"
           expiration-period="10"/>
    ...
    <cache name="DefaultCache"
           tayzgrid-instance="mycache"
           priority="normal"
           expiration-type="sliding"
           expiration-period="15" />
  </caches>
</application-config>

Alternative Way of Configuring TayzGrid as Java Spring Cache

Here's an alternative way of configuring TayzGrid without any code change


<beans xmlns="http://www.springframework.org/schema/beans" ... >
  <bean id="cacheManager"
        class="com...spring.TayzGridCacheManager">
    <property name="tayzGridConfigurationManager"
              ref="tayzgridConfigManager"/>
  </bean>
 
  <bean id="tayzgridConfigManager"
        class="com...spring.configuration.TayzGridConfigurationManager">
    <property name="configFile"
              value="C:\...\tayzgrid-spring.xml" />
  </bean>
  <bean id="collectionKeyGenerator"
        class="cachekeygenerator.TayzGridCollectionKeyGenerator"/>
  ...
  <cache:advice id="collectionCacheAdvice"
                key-generator="collectionKeyGenerator">
    <cache:caching cache="CustomerCollectionCache">
      <cache:cache-evict all-entries="true"
                         method="create"/>
      ...
      <cache:cacheable method="findAll"/>
    </cache:caching>
  </cache:advice>
  ...
  <aop:config>
    <aop:advisor advice-ref="collectionCacheAdvice"
        pointcut="execution(* customerdb.service.CustomerRESTFacade.*(..))"/>
    ...
  </aop:config>
    ...
</beans>