You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/io/caching.adoc
+32-9Lines changed: 32 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,14 @@
4
4
The Spring Framework provides support for transparently adding caching to an application.
5
5
At its core, the abstraction applies caching to methods, thus reducing the number of executions based on the information available in the cache.
6
6
The caching logic is applied transparently, without any interference to the invoker.
7
+
For more details, check the {url-spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference documentation.
8
+
7
9
Spring Boot auto-configures the cache infrastructure as long as caching support is enabled by using the javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] annotation.
8
10
9
-
NOTE: Check the {url-spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference for more details.
11
+
TIP: Avoid adding javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] to the main method's application class.
12
+
Doing so makes caching a mandatory feature, including xref:io/caching.adoc#io.caching.testing[when running a test suite].
10
13
11
-
In a nutshell, to add caching to an operation of your service add the relevant annotation to its method, as shown in the following example:
14
+
To add caching to an operation of your service add the relevant annotation to its method, as shown in the following example:
12
15
13
16
include-code::MyMathService[]
14
17
@@ -48,10 +51,7 @@ If you have not defined a bean of type javadoc:org.springframework.cache.CacheMa
Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
52
-
53
54
TIP: If the javadoc:org.springframework.cache.CacheManager[] is auto-configured by Spring Boot, it is possible to _force_ a particular cache provider by setting the configprop:spring.cache.type[] property.
54
-
Use this property if you need to xref:io/caching.adoc#io.caching.provider.none[use no-op caches] in certain environments (such as tests).
55
55
56
56
TIP: Use the `spring-boot-starter-cache` starter to quickly add basic caching dependencies.
57
57
The starter brings in `spring-context-support`.
@@ -262,10 +262,6 @@ This is similar to the way the "real" cache providers behave if you use an undec
262
262
[[io.caching.provider.none]]
263
263
=== None
264
264
265
-
When javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] is present in your configuration, a suitable cache configuration is expected as well.
266
-
If you have a custom ` org.springframework.cache.CacheManager`, consider defining it in a separate javadoc:org.springframework.context.annotation.Configuration[format=annotation] class so that you can override it if necessary.
267
-
None uses a no-op implementation that is useful in tests, and slice tests use that by default via javadoc:org.springframework.boot.test.autoconfigure.core.AutoConfigureCache[format=annotation].
268
-
269
265
If you need to use a no-op cache rather than the auto-configured cache manager in a certain environment, set the cache type to `none`, as shown in the following example:
270
266
271
267
[configprops,yaml]
@@ -274,3 +270,30 @@ spring:
274
270
cache:
275
271
type: "none"
276
272
----
273
+
274
+
275
+
276
+
[[io.caching.testing]]
277
+
== Testing
278
+
279
+
It is generally useful to use a no-op implementation when running a test suite.
280
+
This section lists a number of strategies that are useful for tests.
281
+
282
+
When a custom javadoc:org.springframework.cache.CacheManager[] is defined, the best option is to make sure that caching configuration is defined in an isolated javadoc:org.springframework.context.annotation.Configuration[format=annotation] class.
283
+
Doing so makes sure that caching is not required by slice tests.
284
+
For tests that enable a full context, such as javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation], an explicit configuration overriding the regular configuration is required.
285
+
286
+
If caching is auto-configured, more options are available.
287
+
Slice tests are annotated with javadoc:org.springframework.boot.test.autoconfigure.core.AutoConfigureCache[format=annotation], which replaces the auto-configured javadoc:org.springframework.cache.CacheManager[] by a no-op implementation.
288
+
Integration tests can also benefit from this feature by annotate the relevant test class as follows:
289
+
290
+
include-code::MyIntegrationTests[]
291
+
292
+
Another option is to force a no-op implementation for the auto-configured javadoc:org.springframework.cache.CacheManager[]:
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/core/AutoConfigureCache.java
0 commit comments