本篇文章主要介紹了springboot整合spring cache範例程式碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
本文介紹如何在springboot中使用預設的spring cache,
##聲明式快取
Spring 定義CacheManager和Cache 介面用來統一不同的快取技術。例如 JCache、 EhCache、 Hazelcast、 Guava、 Redis 等。在使用 Spring 整合 Cache 的時候,我們需要註冊實作的 CacheManager 的 Bean。 Spring Boot 為我們自動配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。預設使用 ConcurrenMapCacheManager
在我們不使用其他第三方快取依賴的時候,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等等。為了示範這個栗子,我只做了線程的延遲操作,當作是查詢資料庫的時間。
實作介面類別:@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")); } }
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'}
開啟快取技術
在程式的入口中加入@ EnableCaching開啟快取技術:##
@SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@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); } } }
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'}
【相關推薦】
1.
以上是java整合spring cache的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!