Springboot Unofficial Tutorials | 13th article: Springboot Integrated Spring cache

Source: Internet
Author: User

Reprint please indicate the source:
http://blog.csdn.net/forezp/article/details/71023614
This article is from Fang Zhibong's blog

This article describes how to use the default spring cache in Springboot.

Declarative caching

Spring defines the CacheManager and cache interfaces to unify different caching techniques. such as Jcache, EhCache, hazelcast, guava, Redis, and so on. When using Spring to integrate the Cache, we need to register the implemented CacheManager Bean.

Spring Boot automatically configures the jcachecacheconfiguration, Ehcachecacheconfiguration, Hazelcastcacheconfiguration, Guavacacheconfiguration, Rediscacheconfiguration, simplecacheconfiguration and so on.

Use Concurrenmapcachemanager by default

When we do not use other third-party cache dependencies, Springboot automatically uses Concurrenmapcachemanager as the cache manager.

Environmental dependency

Introducing 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
publicinterface BookRepository {    Book getByIsbn(String isbn);}

This you can write a very complex data query operations, such as the operation of MySQL, NoSQL and so on. In order to demonstrate this chestnut, I only made a delay of the thread, as a time to query the database.

Implement the interface class:

@Component Public  class simplebookrepository implements bookrepository {    @Override     PublicBookGETBYISBN(String ISBN) {Simulateslowservice ();return NewBook (ISBN,"Some book"); }//Don ' t do this at home    Private void Simulateslowservice() {Try{LongTime = theL        Thread.Sleep (time); }Catch(Interruptedexception e) {Throw NewIllegalStateException (e); }    }}
Test class
@Component Public  class Apprunner implements Commandlinerunner {    Private Static FinalLogger Logger = Loggerfactory.getlogger (Apprunner.class);Private FinalBookrepository bookrepository; Public Apprunner(Bookrepository bookrepository) { This. bookrepository = Bookrepository; }@Override     Public void Run(String ... args)throwsException {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")); }}

To start the program, you will find that the program prints in the console in turn:

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 in 3s. The caching technology has not yet been turned on.

Turn on caching technology

Add @ enablecaching Open cache technology at the entrance of the program:

@SpringBootApplication@EnableCachingpublicclass DemoApplication {    publicstaticvoidmain(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

Add @cacheable annotations where the cache is needed, such as by adding @cacheable ("books") to the GETBYISBN () method, which opens the cache policy, and when the data is cached, it returns the data directly and does not wait to query the database.

@Component Public  class simplebookrepository implements bookrepository {    @Override    @Cacheable("Books") PublicBookGETBYISBN(String ISBN) {Simulateslowservice ();return NewBook (ISBN,"Some book"); }//Don ' t do this at home    Private void Simulateslowservice() {Try{LongTime = theL        Thread.Sleep (time); }Catch(Interruptedexception e) {Throw NewIllegalStateException (e); }    }}

Then start the program and 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 2 data is printed, the program is 3s, and then the data is instantly printed on the console, which indicates that the cache has played a role.

SOURCE Download: https://github.com/forezp/SpringBootLearning

Resources

Caching

Spring Boot Uncover and Combat (ii) data Cache-Quick Start

Excellent article recommendation:
    • More Springboot Tutorials: Springboot Unofficial Tutorials | Article Summary
    • More Springcoud Tutorials: The simplest springcloud tutorials in history | Article Summary

Springboot Unofficial Tutorials | 13th article: Springboot Integrated Spring cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.