| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | = 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` | 
					
						
							| 
									
										
										
										
											2016-03-17 18:44:56 +08:00
										 |  |  | * `Couchbase` | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | * `Redis` | 
					
						
							| 
									
										
										
										
											2016-04-27 16:29:40 +08:00
										 |  |  | * `Caffeine` | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | * Simple provider based on `ConcurrentHashMap` | 
					
						
							| 
									
										
										
										
											2015-08-25 21:07:24 +08:00
										 |  |  | * Generic provider based on `org.springframework.Cache` bean definition(s) | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | The sample defines a simple `CountryService` that caches countries by ISO code. When | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | caching provider is supported. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 23:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | == Using the JSR-107 annotations | 
					
						
							| 
									
										
										
										
											2015-06-03 23:29:57 +08:00
										 |  |  | 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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | NOTE: You can use the JSR-107 annotations with _any_ cache provider; a JSR-107 | 
					
						
							|  |  |  | compliant cache provider is not necessary. | 
					
						
							| 
									
										
										
										
											2015-06-03 23:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | == Using a different cache provider | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | Initially, the project does not define any caching library so the abstraction works | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | on simple `ConcurrentHashMap`-based caches. You can try out your favorite caching | 
					
						
							|  |  |  | library as explained below. | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 21:07:24 +08:00
										 |  |  | === JCache (JSR-107) | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | 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: | 
					
						
							| 
									
										
										
										
											2015-08-25 21:07:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 15:16:17 +08:00
										 |  |  | * `EhCache 3`: add `org.ehcache:ehcache` | 
					
						
							| 
									
										
										
										
											2015-08-25 21:07:24 +08:00
										 |  |  | * `Hazelcast`: add `com.hazelcast:hazelcast` | 
					
						
							|  |  |  | * `Infinispan`: add `org.infinispan:infinispan-jcache` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 02:23:41 +08:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2015-08-25 21:07:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | === EhCache 2.x | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | Simply add the `net.sf.ehcache:ehcache` dependency to the project. Since there is a | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | 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` | 
					
						
							| 
									
										
										
										
											2019-03-27 22:04:32 +08:00
										 |  |  | anymore. Check https://www.ehcache.org/documentation/3.0/xml.html[the documentation] | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TIP: Run sample cache application using EhCache with | 
					
						
							|  |  |  | `$mvn spring-boot:run -Pehcache2`. | 
					
						
							| 
									
										
										
										
											2016-04-27 16:29:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | === Hazelcast | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | Both `com.hazelcast:hazelcast` and `com.hazelcast:hazelcast-spring` should be added | 
					
						
							| 
									
										
										
										
											2017-11-17 01:51:21 +08:00
										 |  |  | to the project to enable support for Hazelcast. Since there is a default | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | `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`. | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | === Infinispan | 
					
						
							| 
									
										
										
										
											2016-06-03 19:13:56 +08:00
										 |  |  | Add the `org.infinispan:infinispan-spring4-embedded` dependency to enable support for | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | Infinispan. There is no default location that Infinispan uses to look for a config | 
					
						
							| 
									
										
										
										
											2017-07-25 16:02:03 +08:00
										 |  |  | file so this sample is configured to use the provider `infinispan.xml` configuration | 
					
						
							| 
									
										
										
										
											2017-06-07 23:41:38 +08:00
										 |  |  | file specified via the `spring.cache.infinispan.config` property. | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 16:53:37 +08:00
										 |  |  | TIP: Run sample cache application using Infinispan with | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | `$mvn spring-boot:run -Pinfinispan`. | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 19:57:47 +08:00
										 |  |  | NOTE: If you want to use the client/server mode or if you need more options, Infinispan | 
					
						
							|  |  |  | provides an official Spring Boot starter, check | 
					
						
							|  |  |  | https://github.com/infinispan/infinispan-spring-boot[the documentation] for more details. | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 18:00:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 18:44:56 +08:00
										 |  |  | === Couchbase | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | Add the `java-client` and `couchbase-spring-cache` dependencies and make sure that | 
					
						
							|  |  |  | you have setup at least a `spring.couchbase.bootstrap-hosts` property. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 16:53:37 +08:00
										 |  |  | TIP: Run sample cache application using Couchbase with | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | `$mvn spring-boot:run -Pcouchbase`. | 
					
						
							| 
									
										
										
										
											2016-03-17 18:44:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | === Redis | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | 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). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 16:53:37 +08:00
										 |  |  | TIP: Run sample cache application using Redis with | 
					
						
							| 
									
										
										
										
											2017-02-06 17:34:22 +08:00
										 |  |  | `$mvn spring-boot:run -Predis`. | 
					
						
							| 
									
										
										
										
											2015-05-29 21:33:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 15:51:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 21:00:43 +08:00
										 |  |  | === Caffeine | 
					
						
							|  |  |  | 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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 16:53:37 +08:00
										 |  |  | TIP: Run sample cache application using Caffeine with | 
					
						
							| 
									
										
										
										
											2017-11-17 01:51:21 +08:00
										 |  |  | `$mvn spring-boot:run -Pcaffeine`. |