@@ -4013,24 +4013,28 @@ relevant annotation to its method:
4013
4013
4014
4014
[source,java,indent=0]
4015
4015
----
4016
- import javax.cache.annotation.CacheResult;
4017
-
4016
+ import org.springframework.cache.annotation.Cacheable
4018
4017
import org.springframework.stereotype.Component;
4019
4018
4020
4019
@Component
4021
4020
public class MathService {
4022
4021
4023
- @CacheResult
4022
+ @Cacheable("piDecimals")
4024
4023
public int computePiDecimal(int i) {
4025
4024
// ...
4026
4025
}
4027
4026
4028
4027
}
4029
4028
----
4030
4029
4031
- NOTE: You can either use the standard JSR-107 (JCache) annotations or Spring's own
4032
- caching annotations transparently. We strongly advise you however to not mix and match
4033
- them.
4030
+ This example demonstrates the use of caching on a potentially costly operation. Before
4031
+ invoking `computePiDecimal`, the abstraction will look for an entry in the `piDecimals`
4032
+ cache matching the `i` argument. If an entry is found, the content in the cache is
4033
+ immediately returned to the caller and the method is not invoked. Otherwise, the method is
4034
+ invoked and the cache is updated before returning the value.
4035
+
4036
+ NOTE: You can also use the standard JSR-107 (JCache) annotations (e.g. `@CacheResult`)
4037
+ transparently. We strongly advise you however to not mix and match them.
4034
4038
4035
4039
TIP: It is also possible to {spring-reference}/#cache-annotations-put[update] or
4036
4040
{spring-reference}/#cache-annotations-evict[evict] data from the cache transparently.
@@ -4044,6 +4048,13 @@ materialized by the `org.springframework.cache.Cache` and
4044
4048
suitable `CacheManager` according to the implementation as long as the caching support is
4045
4049
enabled via the `@EnableCaching` annotation.
4046
4050
4051
+ TIP: If you do not add any specific cache library, Spring Boot will auto-configure a
4052
+ <<boot-features-caching-provider-simple,Simple provider>> that uses simple maps in
4053
+ memory. When a cache is required for an operation (i.e. `piDecimals` in the example
4054
+ above), the provider will create it on-the-fly for you. When you have made up your mind
4055
+ about the cache provider to use, please make sure to read its documentation to figure out
4056
+ how to configure the caches that your application defines.
4057
+
4047
4058
NOTE: If you are using the cache infrastructure with beans that are not interface-based,
4048
4059
make sure to enable the `proxyTargetClass` attribute of `@EnableCaching`.
4049
4060
@@ -4290,7 +4301,14 @@ auto-configuration.
4290
4301
==== Simple
4291
4302
If none of these options worked out, a simple implementation using `ConcurrentHashMap`
4292
4303
as cache store is configured. This is the default if no caching library is present in
4293
- your application.
4304
+ your application. Caches are created on-the-fly by default but you can restrict the list
4305
+ of available caches using the `cache-names` property. For instance, you you want only a
4306
+ `foo` and `bar` caches:
4307
+
4308
+ [source,properties,indent=0]
4309
+ ----
4310
+ spring.cache.cache-names=foo,bar
4311
+ ----
4294
4312
4295
4313
4296
4314
0 commit comments