Code Monkey home page Code Monkey logo

gs-caching's Introduction

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.

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]

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. Technically, Spring Boot doesn’t have anything to auto-configure when it comes to caching but a future version might.

  • @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the SimpleBookRepository.

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.

Spring Boot automatically configures a suitable CacheManager to serve as a provider for the relevant cache. See the Spring Boot documentation for more details.

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.

gs-caching's People

Contributors

bert-r avatar gregturn avatar royclarkson avatar snicoll avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.