This sample demonstrates the caching auto-configuration support. Spring’s caching abstraction is supported by many caching libraries, including:
-
Any compliant
JSR-107
(JCache) provider -
EhCache
-
Hazelcast
-
Infinispan
-
Couchbase
-
Redis
-
Caffeine
-
Simple provider based on
ConcurrentHashMap
-
Generic provider based on
org.springframework.Cache
bean definition(s)
The sample defines a simple CountryService
that caches countries by ISO code. When
the application starts a client invokes the service with a random code every 500ms.
You can look at the /metrics
endpoint to review the cache statistics if your chosen
caching provider is supported.
The sample uses Spring’s cache annotation. If you want to use the JSR-107 annotations
instead, simply add the javax.cache:cache-api
dependency to the project. No further
configuration is necessary.
Note
|
You can use the JSR-107 annotations with any cache provider; a JSR-107 compliant cache provider is not necessary. |
Initially, the project does not define any caching library so the abstraction works
on simple ConcurrentHashMap
-based caches. You can try out your favorite caching
library as explained below.
If you want to configure your cache infrastructure via the standard, you need a
compliant implementation and the JSR-107 api. You first need to add
javax.cache:cache-api
to your project. Then you could try the following:
-
EhCache 3
: addorg.ehcache:ehcache
-
Hazelcast
: addcom.hazelcast:hazelcast
-
Infinispan
: addorg.infinispan:infinispan-jcache
Tip
|
Run sample cache application using JCache and EhCache3 by uncommenting the
spring.cache.jcache.config property and $mvn spring-boot:run -Pehcache .
|
Tip
|
Refer to the documentation of the JSR-107 implementation you want to use: certain
cache providers do not create a default cache on-the-fly if it does not exist so you might
need to update the sample to create the caches on startup or specify the location to the
provider-specific file via the spring.cache.jcache.config property.
|
Note
|
Any other JSR-107 compliant provider is also supported but Spring Boot may not offer a dependency management entry for it. You will have to add it with the version of the library that you want to use. |
Simply add the net.sf.ehcache:ehcache
dependency to the project. Since there is a
default ehcache.xml
configuration file at the root of the classpath,
it is automatically used to configure the underlying CacheManager
.
Note that EhCache 3 uses a different format and doesn’t default to ehcache.xml
anymore. Check the documentation
for more details.
Tip
|
Run sample cache application using EhCache with
$mvn spring-boot:run -Pehcache2 .
|
Both com.hazelcast:hazelcast
and com.hazelcast:hazelcast-spring
should be added
to the project to enable support for Hazelcast. Since there is a default
hazelcast.xml
configuration file at the root of the classpath, it is used to
automatically configure the underlying HazelcastInstance
.
Tip
|
Run sample cache application using Hazelcast with
$mvn spring-boot:run -Phazelcast .
|
Add the org.infinispan:infinispan-spring4-embedded
dependency to enable support for
Infinispan. There is no default location that Infinispan uses to look for a config
file so this sample is configured to use the provider infinispan.xml
configuration
file specified via the spring.cache.infinispan.config
property.
Tip
|
Run sample cache application using Infinispan with
$mvn spring-boot:run -Pinfinispan .
|
Note
|
If you want to use the client/server mode or if you need more options, Infinispan provides an official Spring Boot starter, check the documentation for more details. |
Add the java-client
and couchbase-spring-cache
dependencies and make sure that
you have setup at least a spring.couchbase.bootstrap-hosts
property.
Tip
|
Run sample cache application using Couchbase with
$mvn spring-boot:run -Pcouchbase .
|
Add the spring-boot-starter-data-redis
and make sure it is configured properly (by
default, a redis instance with the default settings is expected on your local box).
Tip
|
Run sample cache application using Redis with
$mvn spring-boot:run -Predis .
|
Simply add the com.github.ben-manes.caffeine:caffeine
dependency to enable support
for Caffeine. You can customize how caches are created in different ways, see
application.properties
for an example and the documentation for more details.
Tip
|
Run sample cache application using Caffeine with
$mvn spring-boot:run -Pcaffeine .
|