|
Note |
|---|---|
|
Check the relevant section of the Spring Framework reference for more details. |
import javax.cache.annotation.CacheResult; import org.springframework.stereotype.Component; @Component public class MathService { @CacheResult public int computePiDecimal(int i) { // ... }
|
Note |
|---|---|
|
You can either use the standard JSR-107 (JCache) annotations or Spring’s own caching annotations transparently. We strongly advise you however to not mix and match them. |
|
Note |
|---|---|
|
If you are using the cache infrastructure with beans that are not interface-based,
make sure to enable the
|
|
Tip |
|---|---|
|
Use the
|
|
Tip |
|---|---|
|
It is also possible to
force
the cache provider to use via the
|
If the
CacheManager
is auto-configured by Spring Boot, you can further tune its
configuration before it is fully initialized by exposing a bean implementing the
CacheManagerCustomizer
interface. The following sets the cache names to use.
@Bean public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() { return new CacheManagerCustomizer<ConcurrentMapCacheManager>() { @Override public void customize(ConcurrentMapCacheManager cacheManager) { cacheManager.setCacheNames(Arrays.asList("one", "two")); }
|
Note |
|---|---|
|
In the example above, a
|
# Only necessary if more than one provider is present spring.cache.jcache.provider=com.acme.MyCachingProvider spring.cache.jcache.config=classpath:acme.xml
|
Note |
|---|---|
|
Since a cache library may offer both a native implementation and JSR-107 support Spring Boot will prefer the JSR-107 support so that the same features are available if you switch to a different JSR-107 implementation. |
|
Tip |
|---|---|
|
Spring Boot has a
general support for Hazelcast
. If
a single
|
There are several ways to customize the underlying
javax.cache.cacheManager
:
spring.cache.cache-names
property. If a
custom
javax.cache.configuration.Configuration
bean is defined, it is used to
customize them.
org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer
beans are
invoked with the reference of the
CacheManager
for full customization.
|
Tip |
|---|---|
|
If a standard
|
Spring Boot has a
general support for Hazelcast
. If
a
HazelcastInstance
has been auto-configured, it is automatically wrapped in a
CacheManager
.
spring.cache.infinispan.config=infinispan.xml
|
Note |
|---|---|
|
The support of Infinispan in Spring Boot is restricted to the embedded mode and is quite basic. If you want more options you should use the official Infinispan Spring Boot starter instead, check the documentation for more details. |
If the Couchbase java client and the
couchbase-spring-cache
implementation are
available and Couchbase is
configured
, a
CouchbaseCacheManager
will be auto-configured. It is also possible to create additional
caches on startup using the
spring.cache.cache-names
property. These will operate on
the
Bucket
that was auto-configured. You can
also
create additional caches on another
Bucket
using the customizer: assume you need two caches on the "main"
Bucket
(
foo
and
bar
) and one
biz
cache with a custom time to live of 2sec on the
another
Bucket
. First, you can create the two first caches simply via configuration:
spring.cache.cache-names=foo,bar
Then define this extra
@Configuration
to configure the extra
Bucket
and the
biz
cache:
@Configuration public class CouchbaseCacheConfiguration { private final Cluster cluster; public CouchbaseCacheConfiguration(Cluster cluster) { this.cluster = cluster; @Bean public Bucket anotherBucket() { return this.cluster.openBucket("another", "secret"); @Bean public CacheManagerCustomizer<CouchbaseCacheManager> cacheManagerCustomizer() { return c -> { c.prepareCache("biz", CacheBuilder.newInstance(anotherBucket()) .withExpiration(2)); }
This sample configuration reuses the
Cluster
that was created via auto-configuration.
|
Note |
|---|---|
|
By default, a key prefix is added to prevent that if two separate caches use the same
key, Redis would have overlapping keys and be likely to return invalid values. We strongly
recommend to keep this setting enabled if you create your own
|