Polish cache sample
Update single cache sample with all supported cache providers. Add README to shortly explain how to get started with them. See gh-2633
This commit is contained in:
72
spring-boot-samples/spring-boot-sample-cache/README.adoc
Normal file
72
spring-boot-samples/spring-boot-sample-cache/README.adoc
Normal file
@@ -0,0 +1,72 @@
|
||||
= Spring Boot Cache Sample
|
||||
|
||||
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`
|
||||
* `Redis`
|
||||
* `Guava`
|
||||
* Simple provider based on `ConcurrentHashMap`
|
||||
|
||||
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.
|
||||
|
||||
== Using a different cache provider
|
||||
|
||||
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.
|
||||
|
||||
=== EhCache 2.x
|
||||
|
||||
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`.
|
||||
|
||||
=== Hazelcast
|
||||
|
||||
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`.
|
||||
|
||||
=== Infinispan
|
||||
|
||||
Simply add the `org.infinispan:infinispan-spring4` dependency to enable support for
|
||||
Infinispan. There is no default location that Infinispan uses to look for a config
|
||||
file so if you don't specify anything it will bootstrap on a hardcoded default. You
|
||||
can set the `spring.cache.infinispan.config` property to use the provided
|
||||
`infinispan.xml` configuration instead.
|
||||
|
||||
=== JCache (JSR-107)
|
||||
|
||||
You do not need to use a JSR-107 compliant `CacheManager` to use the standard
|
||||
annotations. As a matter of a fact, this sample uses by default the standard annotations
|
||||
with a simple map-based `CacheManager` by default. If you want to configure your cache
|
||||
infrastructure via the standard, you need a compliant implementation. You could try
|
||||
the following:
|
||||
|
||||
* `Hazelcast`: add `com.hazelcast:hazelcast`
|
||||
* `Infinispan`: add `org.infinispan:infinispan-jcache`
|
||||
|
||||
|
||||
Since Spring Boot supports the native cache library and the JCache wrapper, you
|
||||
should set the `spring.cache.type` property to `jcache` to specify that you want the
|
||||
cache manager to be auto-configured that way.
|
||||
|
||||
=== Redis
|
||||
|
||||
Add the `spring-boot-starter-redis` and make sure it is configured properly (by default,
|
||||
a redis instance with the default settings is expected on your local box).
|
||||
|
||||
=== Guava
|
||||
|
||||
Spring Boot does not provide any dependency management for _Guava_ so you'll have to add
|
||||
the `com.google.guava:guava` dependency with a version. You can customize how caches are
|
||||
created in different ways, see `application.properties` for an example and the
|
||||
documentation for more details.
|
||||
Reference in New Issue
Block a user