電子商務社交平臺源碼請加企鵝求求:一零三八七七四六二六。本文介紹如何在springboot中使用默認的spring cache,mysql
Spring 定義 CacheManager 和 Cache 接口用來統一不一樣的緩存技術。例如 JCache、 EhCache、 Hazelcast、 Guava、 Redis 等。在使用 Spring 集成 Cache 的時候,咱們須要註冊實現的 CacheManager 的 Bean。spring
Spring Boot 爲咱們自動配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。sql
在咱們不使用其餘第三方緩存依賴的時候,springboot自動採用ConcurrenMapCacheManager做爲緩存管理器。數據庫
在pom文件引入spring-boot-starter-cache環境依賴:緩存
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>複製代碼
public class Book {
private String isbn;
private String title;
public Book(String isbn, String title) {
this.isbn = isbn;
this.title = title;
}
….getter
….setter
}複製代碼
public interface BookRepository {
Book getByIsbn(String isbn);
}複製代碼
這個你能夠寫一個很複雜的數據查詢操做,好比操做mysql、nosql等等。爲了演示這個栗子,我只作了一下線程的延遲操做,看成是查詢數據庫的時間。springboot
實現接口類:bash
@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); } } }複製代碼
@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"));
}
}複製代碼
啓動程序,你會發現程序在控制檯依次打印了:nosql
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’}複製代碼
你會發現程序依次3s打印一行日誌。這時還沒開啓緩存技術。分佈式
在程序的入口中加入@ EnableCaching開啓緩存技術:ide
@SpringBootApplication
@EnableCaching
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}複製代碼
在須要緩存的地方加入@Cacheable註解,好比在getByIsbn()方法上加入@Cacheable(「books」),這個方法就開啓了緩存策略,當緩存有這個數據的時候,會直接返回數據,不會等待去查詢數據庫。
@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); } } }複製代碼
Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六