java版spring cloud+spring boot+redis多租戶社交電子商務平臺 (十三)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>
復制代碼

創建一個book數據訪問層

先創建一個實體類

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 {@Overridepublic Book getByIsbn(String isbn) {simulateSlowService();return new Book(isbn, "Some book");}// Don't do this at homeprivate 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;}@Overridepublic 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 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’}復制代碼

你會發現程序依次3s打印一行日志。這時還沒開啟緩存技術。

開啟緩存技術

在程序的入口中加入@ EnableCaching開啟緩存技術:

@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 homeprivate 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’}復制代碼

只有打印前面2個數據,程序等了3s,之后的數據瞬間打印在控制臺上了,這說明緩存起了作用。
電子商務社交平臺源碼請加企鵝求求:三五三六二四七二五九

轉載于:https://juejin.im/post/5cf8e6346fb9a07f03572b54

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275018.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275018.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275018.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

windows符號服務器地址

當調試windows程序的時候&#xff0c;有時候會需要一些符號文件。系統的公有符號文件微軟都是提供的&#xff0c;只需在調試器中設置即可&#xff0c;在下次調試時&#xff0c;調試器會自動從網上下載需要的符號文件。可以使用符號文件的調試器有windbg等等。 符號服務器地址&a…

如何融入到更積極的環境,促進技術提升

眾所周知&#xff0c;關注公眾號可以了解學習掌握技術方向&#xff0c;學習優質好文&#xff0c;落實到自己項目中。還可以結交圈內好友&#xff0c;讓自己融入到積極上進的技術氛圍&#xff0c;促進自己的技術提升。話不多說&#xff0c;推薦這些優質前端公眾號前端之神100w閱…

動畫 制作_您希望制作的10個醒目的徽標動畫

動畫 制作重點 (Top highlight)標志設計 (Logo Design) Have you ever watched paint dry? No? I didn’t think so. How about watched a turtle crossing the road? Probably not. Maybe spent an hour standing in line at the post office? Well that’s pretty likely…

NOIP訓練營集訓筆記—信息學基礎算法(倍增與分治算法

本文摘自清北OI學堂內部筆記&#xff0c;作者潘愷璠&#xff0c;來自柳鐵一中曾參加過清北訓練營提高組精英班&#xff0c;主要記錄的是信息學基礎算法。筆記非常詳細&#xff0c;特分享給大家&#xff01; NOIP2019年夏令營正在報名中&#xff0c;6大校區10種班型&#xff0c;…

使用 CSS 用戶選擇控制選擇

IE10 平臺預覽 4 包括一個新的 CSS 屬性的支持-ms-user-select&#xff0c;這使得 Web 開發者控制完全可以選擇什么的文本&#xff0c;在其網站上更容易。如果你是看我一整天都在我的工作站&#xff0c;您會注意到我讀計算機上時&#xff0c;我選擇的文本。我不是只有一個人讀起…

一個在校的普通前端小姐姐的2021

大家好&#xff0c;我是若川。這是我的源碼共讀群里一個大三的前端小姐姐&#xff08;小曹同學&#xff09;的年度總結。她寫了5篇源碼筆記。同時做了很多項目&#xff0c;獲得了很多獎。而且策劃和建立了學校工作室的前端訓練營&#xff0c;40人報名參加。總之就是現在的大學生…

按鈕 交互_SwiftUI中的微交互—菜單按鈕動畫

按鈕 交互Microinteractions have become increasingly important in a world with a dizzying number of digital platforms and an ocean of content. While microinteractions used to be considered an interesting resource in the early days of digital design, in toda…

JavaScript邏輯運算符的使用技巧

前言 !, &&, || 三個運算符是JavaScript中重要的邏輯運算符&#xff0c;本文將介紹這三個運算符在JavaScript實際編程中的有趣使用技巧。 取反運算符&#xff08;!&#xff09; 如果對一個值連續做兩次取反運算&#xff0c;等于將其轉為對應的布爾值&#xff0c;與Bool…

如何接觸到最新的前端動態、最前沿的前端技術

眾所周知&#xff0c;關注公眾號可以了解學習掌握技術方向&#xff0c;學習優質好文&#xff0c;落實到自己項目中。還可以結交圈內好友&#xff0c;讓自己融入到積極上進的技術氛圍&#xff0c;促進自己的技術提升。話不多說&#xff0c;推薦這些優質前端公眾號前端有道社區活…

選擇控件— UI組件系列

重點 (Top highlight)The word “toggle” is a reference to a switch with a short handle that alternates between two states each time it is activated. You encounter it every time you “switch” on the lights.單詞“ toggle”是指帶有短手柄的開關&#xff0c;該開…

linux -- Linux diff與patch的深入分析

diff的輸出格式分為傳統格式和統一格式 1)diff的傳統格式輸出. ############################################ cat before.txt 輸出: This is a line to be deleted This is a line that will be changed This is a line that will be unchanged cat after.txt 輸出: This is …

shell命令之---sed

1. sed編輯器基礎 1.1 替換標記 命令格式&#xff1a;s/pattern/replacement/flags $ cat data4.txt    This is a test of the test script.    This is the second test of the test script.    有4種可用的替換標記&#xff1a; 數字&#xff0c;表明新文本將替…

SEE Conf: Umi 4 設計思路文字稿

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。復制此鏈接 https:…

用戶體驗改善案例_改善用戶體驗研究的5種習慣

用戶體驗改善案例There’s plenty of misunderstanding around user research, whether it’s the concept of validation or one-off anecdotes being thrown around as concrete evidence for a product decision.用戶研究存在很多誤解&#xff0c;無論是驗證的概念還是一次性…

一場賽跑引起的并發知識

享學特邀作者&#xff1a;老顧前言我們小伙伴們是不是經常需要測試代碼的性能&#xff1f;小伙伴們是不是就會想到jmeter進行壓力測試一下&#xff0c;模擬N個用戶同時執行下&#xff0c;看看響應的時間多少。今天老顧就用一個經典的比賽案例&#xff0c;來嘗試自行編寫個比賽業…

oracle中使用子查詢為何取到大于自然數1 rownum 淺度解析

Oracle 沒有提供TOP N 語句&#xff0c;若希望按特定條件查詢前N 條記錄&#xff0c;可以使用偽列ROWNUM。 ROWNUM 是對結果集加的一個偽列&#xff0c;即先查到結果集之后再加上去的一個列(注意&#xff1a;先要 有結果集)。 rownum 的值是oracle 順序分配的從查詢返回的行的編…

巴克萊對沖_“巴克萊的財政預算案”:使金錢管理對心理健康有效—用戶體驗案例研究

巴克萊對沖Disclaimer: all official Barclays assets used for this project are purely for educational/project purposes only and do not reflect the intentions of Barclays or any of its affiliates.免責聲明&#xff1a;用于此項目的所有官方巴克萊資產純粹是出于教育…

6 個對所有 Web 開發者都有用的 GitHub 倉庫

作者&#xff1a;Mehdi Aoussiad原文&#xff1a;https://javascript.plainenglish.io/6-useful-github-repositories-for-all-web-developers-44f26912fd66大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&…

快速刪除數據庫中所有表中的數據

今天又學到一招&#xff0c;可以快速刪除數據庫中所有的用戶表中的數據。我是個菜鳥&#xff0c;望各位大神多多指教 select truncate table Name ; from sysobjects where xtypeU order by name asc; 該條語句執行之后會將數據庫中所有的表都查詢出來&#xff0c;復制出來之…

openfiler的iSCSI配置(二)

為什么80%的碼農都做不了架構師&#xff1f;>>> 一.openfiler iSCSI配置 1.啟動iSCSI target server服務。在Services列表下。 2.設置訪問列表。在System---Network Access Configuration下設置。 3.創建卷設備 二.ISCSI客戶端配置 1.安裝open-iscsi # apt-get ins…