This section dives into the Coherence Spring Cache module. It explains how to use Coherence’s support for the Spring Framework’s Cache Abstraction.

1. Introduction

Spring provides its own cache abstraction, allowing you to add caching to Java methods. Coherence Spring provides an implementation of this abstraction for Oracle Coherence.

Spring’s Cache abstraction also supports JSR-107 which is also supported by Oracle Coherence. As such you have another alternative for setting up caching.
If you are using JPA/Hibernate you may also consider using the Coherence support for Hibernate’s second-level cache SPI, which is provided by the Coherence Hibernate project.

2. Configuring Coherence Cache for Spring

As a start, please familiarize yourself with Spring’s Cache Abstraction by reading the following resources:

Spring’s cache abstraction for Coherence will be automatically enabled as soon as you specify @EnableCaching in your applications configuration classes. In that case a CoherenceCacheManager bean implementation is registered as CacheManager. Of course, you can define your own CacheManager as well, but in that case auto-configuration will back-off.

The autoconfiguration logic is defined in class EnableCoherenceImportBeanDefinitionRegistrar.
Example 1. Defining your own CacheManager
Java
@Configuration
@EnableCaching
public class CacheConfiguration {
    @Bean
    public CacheManager cacheManager(Coherence coherence) {
      return new CoherenceCacheManager(coherenceInstance);
    }
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd">

  <cache:annotation-driven/>

  <bean id="cacheManager" class="com.oracle.coherence.spring.cache.CoherenceCacheManager">
    <constructor-arg ref="coherenceInstance"/>
  </bean>
</beans>

When the CacheManager gets instantiated it is configured with default settings that then get applied for all underlying caches being created and used. Those settings are defined using the CoherenceCacheConfiguration object. If not specified as a second (optional) constructor argument to the CoherenceCacheManager, a default instance is created.

Thus, you can customize certain cache properties when instantiating the CoherenceCacheManager by providing your own CoherenceCacheConfiguration instance as a second constructor argument. For example, you can set the time-to-live (ttl) property to specify that caches shall expire after a certain period.

By default, the underling time-to-live property for caches used for the Spring cache abstraction is 0. This means that cache entries typically do not expire. However, you can also specify cache expiration settings via the coherence-cache-config.xml.
Example 2. Defining a CacheManager with additional cache configuration
Java
@Configuration
@EnableCaching
public class CacheConfiguration {
    @Bean
    public CacheManager cacheManager(Coherence coherence) {
        CoherenceCacheConfiguration cacheConfiguration =
                    new CoherenceCacheConfiguration(Duration.ofMillis(1234));
        return new CoherenceCacheManager(coherenceInstance, cacheConfiguration);
    }
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd">

  <cache:annotation-driven/>

  <bean id="cacheManager" class="com.oracle.coherence.spring.cache.CoherenceCacheManager">
    <constructor-arg ref="coherenceInstance"/>
    <constructor-arg>
        <bean class="com.oracle.coherence.spring.cache.CoherenceCacheConfiguration">
            <constructor-arg>
                <bean class="java.time.Duration" factory-method="ofMillis">
                    <constructor-arg value="1234"/>
                </bean>
            </constructor-arg>
        </bean>
    </constructor-arg>
  </bean>
</beans>
You don’t need to define a CoherenceCacheManager to configure the default configuration. You can also just define a CoherenceCacheConfiguration bean in your Spring application context. As long as there is only a single bean of that type defined, the default CoherenceCacheManager that is being implicitly created will use that bean.

Please consult the Quickstart chapter to see an example using Spring’s cache abstraction support with Spring Boot. Coherence Spring also provides an example of using Spring Framework (without Spring Boot). The source code for the samples is part of the Coherence Spring projects:

If you’re using Spring Boot, please continue reading the Spring Boot specific chapter on caching.