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;
Create a data access interface
Public interface 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:
@Componentpublic class Simplebookrepository implements Bookrepository { @Override 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);}} }
Test class
@Componentpublic class Apprunner implements Commandlinerunner { private static final Logger Logger = loggerfactory.ge Tlogger (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"));} }
To start the program, you will find that the program prints in the console in turn:
2014-06-05 12:15:35.783 … : …. Fetching books2014-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 @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 (27) Integrated Spring cache