Add spring-boot-starter-cache

Closes gh-3098
This commit is contained in:
Stephane Nicoll
2015-06-03 17:29:57 +02:00
parent 0ad0ad4c60
commit fca192fa41
9 changed files with 68 additions and 19 deletions

View File

@@ -16,6 +16,15 @@ the application starts a client invokes the service with a random code every 500
can look at the `/metrics` endpoint to review the cache statistics if your chosen
caching provider is supported.
== Using the JSR-107 annotations
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.
== Using a different cache provider
Initially, the project does not define any caching library so the abstraction works
@@ -45,11 +54,8 @@ can set the `spring.cache.infinispan.config` property to use the provided
=== 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:
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`

View File

@@ -20,6 +20,12 @@
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Only used to expose cache metrics -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@@ -28,15 +34,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<!-- Additional cache providers
<dependency>

View File

@@ -16,16 +16,15 @@
package sample;
import javax.cache.annotation.CacheDefaults;
import javax.cache.annotation.CacheResult;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
@CacheDefaults(cacheName = "countries")
@CacheConfig(cacheNames = "countries")
public class CountryRepository {
@CacheResult
@Cacheable
public Country findByCode(String code) {
System.out.println("---> Loading country with code '" + code + "'");
return new Country(code);