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)