Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (34) About integrated Spring Cache (2)

Source: Internet
Author: User

Turn on caching technology

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

@SpringBootApplication @enablecachingpublic class DemoApplication {public    static void Main (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.

@Componentpublic class Simplebookrepository implements Bookrepository {    @Override    @Cacheable ("books")    Public book GETBYISBN (String ISBN) {        simulateslowservice ();        Return new book (ISBN, "Some book");    Don ' t do the at home    private void Simulateslowservice () {        try {            long time = 3000L;            Thread.Sleep (time);        } catch (Interruptedexception e) {            throw new illegalstateexception (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 '}

  

Source Source

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.

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (34) About integrated Spring Cache (2)

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.