This guide walks you through the process of enabling caching on a Spring managed bean.
If you use Maven, visit the Spring Initializr to generate a new project with the required dependencies (Spring Batch and HyperSQL Database).
The following listing shows the pom.xml
file created when you choose Maven:
link:complete/pom.xml[role=include]
If you use Gradle, visit the Spring Initializr to generate a new project with the required dependencies (Spring Batch and HyperSQL Database).
The following listing shows the build.gradle
file created when you choose Gradle:
link:complete/build.gradle[role=include]
If you want to initialize the project manually rather than use the links shown earlier, follow the steps given below:
Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.
Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
Click Dependencies and select Spring cache abstraction.
Click Generate.
Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
Note
|
If your IDE has the Spring Initializr integration, you can complete this process from your IDE. |
First, you need to create a simple model for your book. The following listing (from
src/main/java/com/example/caching/Book.java
) shows how to do so:
link:initial/src/main/java/com/example/caching/Book.java[role=include]
You also need a repository for that model. The following listing (from
src/main/java/com/example/caching/BookRepository.java
) shows such a repository:
link:initial/src/main/java/com/example/caching/BookRepository.java[role=include]
You could have used Spring Data to provide an implementation of your
repository over a wide range of SQL or NoSQL stores. However, for the purpose of this
guide, you will simply use a naive implementation that simulates some latency
(network service, slow delay, or other issues). The following listing (from
src/main/java/com/example/caching/SimpleBookRepository.java
) shows such a repository:
link:initial/src/main/java/com/example/caching/SimpleBookRepository.java[role=include]
simulateSlowService
deliberately inserts a three-second delay into each getByIsbn
call. Later on, you will speed up this example with caching.
Next, you need to wire up the repository and use it to access some books. The following
listing (from src/main/java/com/example/caching/CachingApplication.java
) shows how to do
so:
link:initial/src/main/java/com/example/caching/CachingApplication.java[role=include]
You also need a CommandLineRunner
that injects the BookRepository
and calls
it several times with different arguments. The following listing (from
src/main/java/com/example/caching/AppRunner.java
) shows that class:
link:complete/src/main/java/com/example/caching/AppRunner.java[role=include]
If you try to run the application at this point, you should notice that it is quite slow, even though you are retrieving the exact same book several times. The following sample output shows the three-second delay that our (intentionally awful) code created:
2014-06-05 12:15:35.783 ... : .... Fetching books 2014-06-05 12:15:40.783 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'} 2014-06-05 12:15:43.784 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'} 2014-06-05 12:15:46.786 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
We can improve the situation by enabling caching.
Now you can enable caching on your SimpleBookRepository
so that the books are cached
within the books
cache. The following listing (from
src/main/java/com/example/caching/SimpleBookRepository.java
) shows the repository
definition:
link:complete/src/main/java/com/example/caching/SimpleBookRepository.java[role=include]
You now need to enable the processing of the caching annotations, as the following example
(from src/main/java/com/example/caching/CachingApplication.java
) shows how to do:
link:complete/src/main/java/com/example/caching/CachingApplication.java[role=include]
The @EnableCaching
annotation triggers a post-processor that
inspects every Spring bean for the presence of caching annotations on public
methods. If such an annotation is found, a proxy is automatically created to intercept
the method call and handle the caching behavior accordingly.
The post-processor handles the @Cacheable
, @CachePut
and
@CacheEvict
annotations. You can refer to the Javadoc and
the reference guide for more detail.
Spring Boot automatically configures a suitable CacheManager
to serve as
a provider for the relevant cache. See the Spring Boot documentation for
more detail.
Our sample does not use a specific caching library, so our cache store is the simple
fallback that uses ConcurrentHashMap
. The caching abstraction supports a wide range of
cache libraries and is fully compliant with JSR-107 (JCache).
Now that caching is enabled, you can run the application again and see the difference by adding additional calls with or without the same ISBN. It should make a huge difference. The following listing shows the output with caching enabled:
2016-09-01 11:12:47.033 .. : .... Fetching books 2016-09-01 11:12:50.039 .. : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'} 2016-09-01 11:12:53.044 .. : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'} 2016-09-01 11:12:53.045 .. : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'} 2016-09-01 11:12:53.045 .. : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'} 2016-09-01 11:12:53.045 .. : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'} 2016-09-01 11:12:53.045 .. : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
In the preceding sample output, the first retrieval of a book still takes three seconds. However, the second and subsequent times for the same book are much faster, showing that the cache is doing its job.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。