This article describes how to use the default spring cache in spring boot.
Declarative Caching
Spring defines the CacheManager and Cache interfaces to unify different caching technologies. For example, JCache, EhCache, Hazelcast, Guava, Redis, etc. When using Spring to integrate Cache, we need to register the Bean of the Cache Manager implemented.
Spring Boot automatically configures Jcache Cache Configuration, EhCache Configuration, Hazelcast Cache Configuration, Guava Cache Configuration, RedisCache Configuration, SimpleCache Configuration, etc.
Concurrent MapCache Manager is used by default
springboot automatically uses Concurrent MapCache Manager as the cache manager when we do not use other third-party cache dependencies.
Environmental dependence
Introduce spring-boot-starter-cache environment dependencies in pom files:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
Create a book data access layer
First create an entity class
public class Book { private String isbn; private String title; public Book(String isbn, String title) { this.isbn = isbn; this.title = title; } ….getter ….setter }
Create a Data Access Interface
public interface BookRepository { Book getByIsbn(String isbn); }
You can write a very complex data query operation, such as mysql, nosql and so on. In order to demonstrate this chestnut, I only did a thread delay operation as the time to query the database.
Implementing interface classes:
@Component public class SimpleBookRepository implements BookRepository { @Override public Book getByIsbn(String isbn) { simulateSlowService(); return new Book(isbn, "Some book"); } // Don't do this at home private void simulateSlowService() { try { long time = 3000L; Thread.sleep(time); } catch (InterruptedException e) { throw new IllegalStateException(e); } } }
Test class
@Component public class AppRunner implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(AppRunner.class); private final BookRepository bookRepository; public AppRunner(BookRepository bookRepository) { this.bookRepository = bookRepository; } @Override public void run(String... args) throws Exception { logger.info(".... Fetching books"); logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567")); logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567")); logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); } }
Start the program and you will find that the program is printed in turn on the console:
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'}
You will find that the program prints a line of logs for 3s in turn. Caching technology has not yet been turned on.
Open Caching Technology
Add @ EnableCaching to the program entry to open the cache technology:
@SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Add @Cacheable annotations where caching is needed, such as @Cacheable("books") on the getByIsbn () method, which opens the caching strategy. When caching has this data, it returns the data directly and does not wait to query the database.
@Component public class SimpleBookRepository implements BookRepository { @Override @Cacheable("books") public Book getByIsbn(String isbn) { simulateSlowService(); return new Book(isbn, "Some book"); } // Don't do this at home private void simulateSlowService() { try { long time = 3000L; Thread.sleep(time); } catch (InterruptedException e) { throw new IllegalStateException(e); } } }
When you start the program again, you will find that the program prints:
isbn-1234 –>Book{isbn='isbn-1234', title='Some book'} 2017-04-23 18:17:09.479 INFO 8054 — [ main] forezp.AppRunner : isbn-4567 –>Book{isbn='isbn-4567', title='Some book'} 2017-04-23 18:17:09.480 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn='isbn-1234', title='Some book'} 2017-04-23 18:17:09.480 INFO 8054 — [ main] forezp.AppRunner : isbn-4567 –>Book{isbn='isbn-4567', title='Some book'} 2017-04-23 18:17:09.481 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn='isbn-1234', title='Some book'} 2017-04-23 18:17:09.481 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn='isbn-1234', title='Some book'}
Only the first two data are printed, the program waits for 3 seconds, and then the data is printed instantaneously on the console, which shows that caching plays a role.
Spring Cloud Large Enterprise Distributed Microservice Cloud Architecture Source Code Plus Penguin Request: 1791 143380