The cache abstraction does not provide an actual store and relies on abstraction
materialized by the
org.springframework.cache.Cache
and
org.springframework.cache.CacheManager
interfaces. Spring Boot auto-configures a
suitable
CacheManager
according to the implementation as long as the caching support is
enabled via the
@EnableCaching
annotation.
|
Note
|
If you are using the cache infrastructure with beans that are not interface-based,
make sure to enable the
proxyTargetClass
attribute of
@EnableCaching
.
|
|
Tip
|
Use the
spring-boot-starter-cache
‘Starter’ to quickly add basic caching
dependencies. The starter brings
spring-context-support
: if you are adding dependencies
manually, you must include it if you intend to use the JCache, EhCache 2.x or Guava
support.
|
If you haven’t defined a bean of type
CacheManager
or a
CacheResolver
named
cacheResolver
(see
CachingConfigurer
), Spring Boot tries to detect the following
providers (in this order):
|
Tip
|
It is also possible to
force
the cache provider to use via the
spring.cache.type
property. Use this property if you need to
disable
caching altogether
in certain environment (e.g. tests).
|
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
ConcurrentMapCacheManager
is expected to be configured. If that
is not the case, the customizer won’t be invoked at all. You can have as many customizers
as you want and you can also order them as usual using
@Order
or
Ordered
.
|
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.