tags | projects | ||
---|---|---|---|
|
|
This guide walks you through the process of enabling caching on a Spring managed bean.
First, let’s create a very simple model for your book
src/main/java/hello/Book.java
link:initial/src/main/java/hello/Book.java[role=include]
And a repository for that model:
src/main/java/hello/BookRepository.java
link:initial/src/main/java/hello/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, but for the purpose of this guide, you will simply use a naive implementation that simulates some latency (network service, slow delay, etc).
src/main/java/hello/SimpleBookRepository.java
link:initial/src/main/java/hello/SimpleBookRepository.java[role=include]
simulateSlowService
is deliberately inserting a three second delay into each getByIsbn
call. This is an example that later on, you’ll speed up with caching.
Next, wire up the repository and use it to access some books.
src/main/java/hello/Application.java
link:initial/src/main/java/hello/Application.java[role=include]
There is also a CommandLineRunner
that injects the BookRepository
and calls it several times with different arguments.
src/main/java/hello/AppRunner.java
link:complete/src/main/java/hello/AppRunner.java[role=include]
If you try to run the application at this point, you’ll notice it’s quite slow even though you are retrieving the exact same book several times.
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'}
As can be seen by the timestamps, each book took about three seconds to retrieve, even though it’s the same title being repeatedly fetched.
Let’s enable caching on your SimpleBookRepository
so that the books are cached within
the books
cache.
src/main/java/hello/SimpleBookRepository.java
link:complete/src/main/java/hello/SimpleBookRepository.java[role=include]
You now need to enable the processing of the caching annotations
src/main/java/hello/Application.java
link:complete/src/main/java/hello/Application.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 annotations that are managed by this post processor are Cacheable
,
CachePut
and CacheEvict
. You can refer to the javadocs and
the documentation for more details.
Spring Boot automatically configures a suitable CacheManager
to serve as
a provider for the relevant cache. See the Spring Boot documentation for more details.
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 library and is fully compliant with JSR-107 (JCache).
Now that caching is enabled, you can execute it again and see the difference by adding additional calls with or without the same isbn. It should make a huge difference.
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'}
This excerpt from the console shows that the first time to fetch each title took three seconds, but each subsequent call was near instantaneous.
Congratulations! You’ve just enabled caching on a Spring managed bean.