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:
-
The Cache Abstraction chapter in the core Spring Framework reference guide
-
Spring Boot’s reference documentation’s support regarding caching
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. |
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public CacheManager cacheManager(Coherence coherence) {
return new CoherenceCacheManager(coherenceInstance);
}
}
<?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>
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.