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>
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 .
|
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public CacheManager cacheManager(Coherence coherence) {
CoherenceCacheConfiguration cacheConfiguration =
new CoherenceCacheConfiguration(Duration.ofMillis(1234));
return new CoherenceCacheManager(coherenceInstance, cacheConfiguration);
}
}
<?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.
3. Coherence Caches and Locking
Oracle Coherence supports the explicit concurrency control of cache entries using locks. The configuration class
CoherenceCacheConfiguration
provides 3 options to specify the relevant settings:
useLocks
This option lets you specify whether you want to use locks or not. This options defaults to true
, meaning that locking is
enabled by default.
When using caching via Coherence*Extend or gRPC, you should disable locking as it is not supported. |
lockTimeout
When locking cache entries, you can set a cache timeout. The specified value in milliseconds, instructs Coherence how
long it shall try to acquire a lock for the cache entry. If a timeout occurs, Coherence Spring will throw an exception.
A value of -1
will cause Coherence to block indefinitely. Specifying 0
will cause Oracle Coherence not to wait at all.
By default, the value is set to 0
.
lockEntireCache
While generally not recommended, the option exists to also lock they entire cache (not just the specific cache entry).
This option defaults to false
.
For further information, please read the corresponding chapter Using Explicit Locking for Data Concurrency in the Oracle Coherence reference guide.