springboot集成spring cache緩存示例代碼
本文介紹如何在springboot中使用默認(rèn)的spring cache,
聲明式緩存
Spring 定義 CacheManager 和 Cache 接口用來統(tǒng)一不同的緩存技術(shù)。例如 JCache、 EhCache、 Hazelcast、 Guava、 Redis 等。在使用 Spring 集成 Cache 的時(shí)候,我們需要注冊(cè)實(shí)現(xiàn)的 CacheManager 的 Bean。
Spring Boot 為我們自動(dòng)配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。
默認(rèn)使用 ConcurrenMapCacheManager
在我們不使用其他第三方緩存依賴的時(shí)候,springboot自動(dòng)采用ConcurrenMapCacheManager作為緩存管理器。
環(huán)境依賴
在pom文件引入spring-boot-starter-cache環(huán)境依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
創(chuàng)建一個(gè)book數(shù)據(jù)訪問層
先創(chuàng)建一個(gè)實(shí)體類
public class Book { private String isbn; private String title; public Book(String isbn, String title) { this.isbn = isbn; this.title = title; } ….getter ….setter }
創(chuàng)建一個(gè)數(shù)據(jù)訪問接口
public interface BookRepository { Book getByIsbn(String isbn); }
這個(gè)你可以寫一個(gè)很復(fù)雜的數(shù)據(jù)查詢操作,比如操作MySQL、nosql等等。為了演示這個(gè)栗子,我只做了一下線程的延遲操作,當(dāng)作是查詢數(shù)據(jù)庫的時(shí)間。
實(shí)現(xiàn)接口類:
@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); } } }
測(cè)試類
@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")); } }
啟動(dòng)程序,你會(huì)發(fā)現(xiàn)程序在控制臺(tái)依次打印了:
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'}
你會(huì)發(fā)現(xiàn)程序依次3s打印一行日志。這時(shí)還沒開啟緩存技術(shù)。
開啟緩存技術(shù)
在程序的入口中加入@ EnableCaching開啟緩存技術(shù):
@SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
在需要緩存的地方加入@Cacheable注解,比如在getByIsbn()方法上加入@Cacheable(“books”),這個(gè)方法就開啟了緩存策略,當(dāng)緩存有這個(gè)數(shù)據(jù)的時(shí)候,會(huì)直接返回?cái)?shù)據(jù),不會(huì)等待去查詢數(shù)據(jù)庫。
@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); } } }
這時(shí)再啟動(dòng)程序,你會(huì)發(fā)現(xiàn)程序打印:
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'}
只有打印前面2個(gè)數(shù)據(jù),程序等了3s,之后的數(shù)據(jù)瞬間打印在控制臺(tái)上了,這說明緩存起了作用。
源碼下載:https://github.com/forezp/SpringBootLearning
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot工具類ReflectionUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之ReflectionUtils的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12Java獲取XML節(jié)點(diǎn)總結(jié)之讀取XML文檔節(jié)點(diǎn)的方法
下面小編就為大家?guī)硪黄狫ava獲取XML節(jié)點(diǎn)總結(jié)之讀取XML文檔節(jié)點(diǎn)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Java使用過濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)
這篇文章主要介紹了Java使用過濾器防止SQL注入XSS腳本注入,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01shiro與spring?security用自定義異常處理401錯(cuò)誤
這篇文章主要介紹了shiro與spring?security用自定義異常處理401錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Spring-data-JPA使用時(shí)碰到的問題以及解決方案
這篇文章主要介紹了Spring-data-JPA使用時(shí)碰到的問題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java如何設(shè)置過期時(shí)間的map的幾種方法
本文主要介紹了Java如何設(shè)置過期時(shí)間的map的幾種方法,常見的解決方法有:ExpiringMap、LoadingCache及基于HashMap的封裝三種,下面就詳細(xì)的介紹一下,感興趣的可以了解下2022-03-03Java web Hibernate如何與數(shù)據(jù)庫鏈接
這篇文章主要介紹了Java web Hibernate如何與數(shù)據(jù)庫鏈接,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06