Commit fca192fa authored by Stephane Nicoll's avatar Stephane Nicoll

Add spring-boot-starter-cache

Closes gh-3098
parent 0ad0ad4c
...@@ -230,6 +230,11 @@ ...@@ -230,6 +230,11 @@
<artifactId>spring-boot-starter-batch</artifactId> <artifactId>spring-boot-starter-batch</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version> <version>1.3.0.BUILD-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId> <artifactId>spring-boot-starter-cloud-connectors</artifactId>
......
...@@ -2331,6 +2331,9 @@ TIP: It is also possible to {spring-reference}/#cache-annotations-put[update] or ...@@ -2331,6 +2331,9 @@ TIP: It is also possible to {spring-reference}/#cache-annotations-put[update] or
=== Supported cache providers === Supported cache providers
NOTE: To easily get started, just add `spring-boot-starter-cache` to the dependencies of
your application.
The cache abstraction does not provide an actual store and relies on a abstraction The cache abstraction does not provide an actual store and relies on a abstraction
materialized by the `org.springframework.cache.Cache` and materialized by the `org.springframework.cache.Cache` and
`org.springframework.cache.CacheManager` interfaces. Spring Boot auto-configures a `org.springframework.cache.CacheManager` interfaces. Spring Boot auto-configures a
......
...@@ -267,6 +267,9 @@ The following application starters are provided by Spring Boot under the ...@@ -267,6 +267,9 @@ The following application starters are provided by Spring Boot under the
|`spring-boot-starter-batch` |`spring-boot-starter-batch`
|Support for "`Spring Batch`" including HSQLDB database. |Support for "`Spring Batch`" including HSQLDB database.
|`spring-boot-starter-cache`
|Support for Spring's Cache abstraction.
|`spring-boot-starter-cloud-connectors` |`spring-boot-starter-cloud-connectors`
|Support for "`Spring Cloud Connectors`" which simplifies connecting to services in cloud |Support for "`Spring Cloud Connectors`" which simplifies connecting to services in cloud
platforms like Cloud Foundry and Heroku. platforms like Cloud Foundry and Heroku.
......
...@@ -16,6 +16,15 @@ the application starts a client invokes the service with a random code every 500 ...@@ -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 can look at the `/metrics` endpoint to review the cache statistics if your chosen
caching provider is supported. 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 == Using a different cache provider
Initially, the project does not define any caching library so the abstraction works 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 ...@@ -45,11 +54,8 @@ can set the `spring.cache.infinispan.config` property to use the provided
=== JCache (JSR-107) === JCache (JSR-107)
You do not need to use a JSR-107 compliant `CacheManager` to use the standard If you want to configure your cache infrastructure via the standard, you need a compliant
annotations. As a matter of a fact, this sample uses by default the standard annotations implementation. You could try the following:
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` * `Hazelcast`: add `com.hazelcast:hazelcast`
* `Infinispan`: add `org.infinispan:infinispan-jcache` * `Infinispan`: add `org.infinispan:infinispan-jcache`
......
...@@ -22,22 +22,19 @@ ...@@ -22,22 +22,19 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-cache</artifactId>
</dependency> </dependency>
<!-- Only used to expose cache metrics -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.cache</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>cache-api</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<!-- Additional cache providers <!-- Additional cache providers
<dependency> <dependency>
<groupId>net.sf.ehcache</groupId> <groupId>net.sf.ehcache</groupId>
......
...@@ -16,16 +16,15 @@ ...@@ -16,16 +16,15 @@
package sample; package sample;
import javax.cache.annotation.CacheDefaults; import org.springframework.cache.annotation.CacheConfig;
import javax.cache.annotation.CacheResult; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@CacheDefaults(cacheName = "countries") @CacheConfig(cacheNames = "countries")
public class CountryRepository { public class CountryRepository {
@CacheResult @Cacheable
public Country findByCode(String code) { public Country findByCode(String code) {
System.out.println("---> Loading country with code '" + code + "'"); System.out.println("---> Loading country with code '" + code + "'");
return new Country(code); return new Country(code);
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
<module>spring-boot-starter-amqp</module> <module>spring-boot-starter-amqp</module>
<module>spring-boot-starter-aop</module> <module>spring-boot-starter-aop</module>
<module>spring-boot-starter-batch</module> <module>spring-boot-starter-batch</module>
<module>spring-boot-starter-cache</module>
<module>spring-boot-starter-cloud-connectors</module> <module>spring-boot-starter-cloud-connectors</module>
<module>spring-boot-starter-data-elasticsearch</module> <module>spring-boot-starter-data-elasticsearch</module>
<module>spring-boot-starter-data-gemfire</module> <module>spring-boot-starter-data-gemfire</module>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-starter-cache</artifactId>
<name>Spring Boot Cache Starter</name>
<description>Spring Boot Cache Starter</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
</dependencies>
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment