/gs-caching

Caching Data with Spring :: Learn how to cache data in memory with Spring

Primary LanguageJava

tags projects
caching
spring-framework

This guide walks you through the process of enabling caching on a Spring managed bean.

What you’ll build

You’ll build an application that enables caching on a simple book repository.

What you’ll need

Create a book repository

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 five second delay into each getByIsbn call. This is an example that later on, you’ll speed up with caching.

Using the repository

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]

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:45.784  ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2014-06-05 12:15:50.786  ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}

As can be seen by the timestamps, each book took about five seconds to retrieve, even though it’s the same title being repeatedly fetched.

Note
This application using Spring Boot’s CommandLineRunner. This class makes it super simple to write code that runs once the application context has been configured.

Enable caching

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.

In its most basic setup, the annotation requires a CacheManager to serve as a provider for the relevant cache. In this example you use an implementation that delegates to a ConcurrentHashMap. The CachingConfigurer interface provides more advanced configuration options.

Test the application

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.

2014-06-05 12:09:23.862 ... : .... Fetching books
2014-06-05 12:09:28.866 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2014-06-05 12:09:33.867 ... : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2014-06-05 12:09:33.867 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2014-06-05 12:09:33.867 ... : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2014-06-05 12:09:33.868 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2014-06-05 12:09:33.868 ... : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}

This excerpt from the console shows that the first time to fetch each title took five seconds the first time, but each subsequent call was near instantaneous.

Summary

Congratulations! You’ve just enabled caching on a Spring managed bean.