This guide walks you through the process of enabling caching on a Spring managed bean.
What You Will build
You will build an application that enables caching on a simple book repository.
What You need
Starting with Spring Initializr
For all Spring applications, you should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the set up for you. This example needs only the Spring cache abstraction dependency. The following image shows the Initializr set up for this sample project:
Note
|
The preceding image shows the Initializr with Maven chosen as the build tool. You
can also use Gradle. It also shows values of com.example and
caching as the Group and Artifact, respectively. You will use those values throughout
the rest of this sample.
|
The following listing shows the pom.xml
file created when you choose Maven:
link:complete/pom.xml[]
The following listing shows the build.gradle
file created when you choose Gradle:
link:complete/build.gradle[]
Create a Book Repository
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[]
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[]
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[]
simulateSlowService
deliberately inserts a three-second delay into each getByIsbn
call. Later on, you will speed up this example with caching.
Using the Repository
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[]
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[]
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.
Enable 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[]
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[]
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).
Test the Application
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.
Summary
Congratulations! You’ve just enabled caching on a Spring managed bean.