日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

瀏覽:110日期:2023-07-26 10:36:04

環(huán)境配置:

JDK 版本:1.8 Caffeine 版本:2.8.0 SpringBoot 版本:2.2.2.RELEASE一、本地緩存介紹

緩存在日常開發(fā)中啟動至關(guān)重要的作用,由于是存儲在內(nèi)存中,數(shù)據(jù)的讀取速度是非常快的,能大量減少對數(shù)據(jù)庫的訪問,減少數(shù)據(jù)庫的壓力。

之前介紹過 Redis 這種 NoSql 作為緩存組件,它能夠很好的作為分布式緩存組件提供多個(gè)服務(wù)間的緩存,但是 Redis 這種還是需要網(wǎng)絡(luò)開銷,增加時(shí)耗。本地緩存是直接從本地內(nèi)存中讀取,沒有網(wǎng)絡(luò)開銷,例如秒殺系統(tǒng)或者數(shù)據(jù)量小的緩存等,比遠(yuǎn)程緩存更合適。

二、緩存組件 Caffeine 介紹

按 Caffeine Github 文檔描述,Caffeine 是基于 JAVA 8 的高性能緩存庫。并且在 spring5 (springboot 2.x) 后,spring 官方放棄了 Guava,而使用了性能更優(yōu)秀的 Caffeine 作為默認(rèn)緩存組件。

1、Caffeine 性能

可以通過下圖觀測到,在下面緩存組件中 Caffeine 性能是其中最好的。

Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

2、Caffeine 配置說明

參數(shù) 類型 描述 initialCapacity integer 初始的緩存空間大小 maximumSize long 緩存的最大條數(shù) maximumWeight long 緩存的最大權(quán)重 expireAfterAccess duration 最后一次寫入或訪問后經(jīng)過固定時(shí)間過期 refreshAfterWrite duration 最后一次寫入后經(jīng)過固定時(shí)間過期 refreshAfterWrite duration 創(chuàng)建緩存或者最近一次更新緩存后經(jīng)過固定的時(shí)間間隔,刷新緩存 weakKeys boolean 打開 key 的弱引用 weakValues boolean 打開 value 的弱引用 softValues boolean 打開 value 的軟引用 recordStats - 開發(fā)統(tǒng)計(jì)功能

注意:

weakValues 和 softValues 不可以同時(shí)使用。 maximumSize 和 maximumWeight 不可以同時(shí)使用。 expireAfterWrite 和 expireAfterAccess 同事存在時(shí),以 expireAfterWrite 為準(zhǔn)。3、軟引用與弱引用 軟引用: 如果一個(gè)對象只具有軟引用,則內(nèi)存空間足夠,垃圾回收器就不會回收它;如果內(nèi)存空間不足了,就會回收這些對象的內(nèi)存。 弱引用: 弱引用的對象擁有更短暫的生命周期。在垃圾回收器線程掃描它所管轄的內(nèi)存區(qū)域的過程中,一旦發(fā)現(xiàn)了只具有弱引用的對象,不管當(dāng)前內(nèi)存空間足夠與否,都會回收它的內(nèi)存

// 軟引用Caffeine.newBuilder().softValues().build();// 弱引用Caffeine.newBuilder().weakKeys().weakValues().build();三、SpringBoot 集成 Caffeine 兩種方式

SpringBoot 有倆種使用 Caffeine 作為緩存的方式:

方式一: 直接引入 Caffeine 依賴,然后使用 Caffeine 方法實(shí)現(xiàn)緩存。

方式二: 引入 Caffeine 和 Spring Cache 依賴,使用 SpringCache 注解方法實(shí)現(xiàn)緩存。

下面將介紹下,這倆中集成方式都是如何實(shí)現(xiàn)的。

Spring Boot 基礎(chǔ)就不介紹了,推薦看下這個(gè)教程:

https://github.com/javastacks/spring-boot-best-practice

四、SpringBoot 集成Caffeine 方式一

1、Maven 引入相關(guān)依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <groupId>mydlq.club</groupId> <artifactId>springboot-caffeine-cache-example-1</artifactId> <version>0.0.1</version> <name>springboot-caffeine-cache-example-1</name> <description>Demo project for Spring Boot Cache</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

2、配置緩存配置類

import com.github.benmanes.caffeine.cache.Cache;import com.github.benmanes.caffeine.cache.Caffeine;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.concurrent.TimeUnit;@Configurationpublic class CacheConfig { @Bean public Cache<String, Object> caffeineCache() { return Caffeine.newBuilder() // 設(shè)置最后一次寫入或訪問后經(jīng)過固定時(shí)間過期 .expireAfterWrite(60, TimeUnit.SECONDS) // 初始的緩存空間大小 .initialCapacity(100) // 緩存的最大條數(shù) .maximumSize(1000) .build(); }}

3、定義測試的實(shí)體對象

import lombok.Data;import lombok.ToString;@Data@ToStringpublic class UserInfo { private Integer id; private String name; private String sex; private Integer age;}

4、定義服務(wù)接口類和實(shí)現(xiàn)類

UserInfoService

import mydlq.club.example.entity.UserInfo;public interface UserInfoService { /** * 增加用戶信息 * * @param userInfo 用戶信息 */ void addUserInfo(UserInfo userInfo); /** * 獲取用戶信息 * * @param id 用戶ID * @return 用戶信息 */ UserInfo getByName(Integer id); /** * 修改用戶信息 * * @param userInfo 用戶信息 * @return 用戶信息 */ UserInfo updateUserInfo(UserInfo userInfo); /** * 刪除用戶信息 * * @param id 用戶ID */ void deleteById(Integer id);}

UserInfoServiceImpl

import com.github.benmanes.caffeine.cache.Cache;import lombok.extern.slf4j.Slf4j;import mydlq.club.example.entity.UserInfo;import mydlq.club.example.service.UserInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.util.StringUtils;import java.util.HashMap;@Slf4j@Servicepublic class UserInfoServiceImpl implements UserInfoService { /** * 模擬數(shù)據(jù)庫存儲數(shù)據(jù) */ private HashMap<Integer, UserInfo> userInfoMap = new HashMap<>(); @Autowired Cache<String, Object> caffeineCache; @Override public void addUserInfo(UserInfo userInfo) { log.info('create'); userInfoMap.put(userInfo.getId(), userInfo); // 加入緩存 caffeineCache.put(String.valueOf(userInfo.getId()),userInfo); } @Override public UserInfo getByName(Integer id) { // 先從緩存讀取 caffeineCache.getIfPresent(id); UserInfo userInfo = (UserInfo) caffeineCache.asMap().get(String.valueOf(id)); if (userInfo != null){ return userInfo; } // 如果緩存中不存在,則從庫中查找 log.info('get'); userInfo = userInfoMap.get(id); // 如果用戶信息不為空,則加入緩存 if (userInfo != null){ caffeineCache.put(String.valueOf(userInfo.getId()),userInfo); } return userInfo; } @Override public UserInfo updateUserInfo(UserInfo userInfo) { log.info('update'); if (!userInfoMap.containsKey(userInfo.getId())) { return null; } // 取舊的值 UserInfo oldUserInfo = userInfoMap.get(userInfo.getId()); // 替換內(nèi)容 if (!StringUtils.isEmpty(oldUserInfo.getAge())) { oldUserInfo.setAge(userInfo.getAge()); } if (!StringUtils.isEmpty(oldUserInfo.getName())) { oldUserInfo.setName(userInfo.getName()); } if (!StringUtils.isEmpty(oldUserInfo.getSex())) { oldUserInfo.setSex(userInfo.getSex()); } // 將新的對象存儲,更新舊對象信息 userInfoMap.put(oldUserInfo.getId(), oldUserInfo); // 替換緩存中的值 caffeineCache.put(String.valueOf(oldUserInfo.getId()),oldUserInfo); return oldUserInfo; } @Override public void deleteById(Integer id) { log.info('delete'); userInfoMap.remove(id); // 從緩存中刪除 caffeineCache.asMap().remove(String.valueOf(id)); }}

5、測試的 Controller 類

import mydlq.club.example.entity.UserInfo;import mydlq.club.example.service.UserInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@RestController@RequestMappingpublic class UserInfoController { @Autowired private UserInfoService userInfoService; @GetMapping('/userInfo/{id}') public Object getUserInfo(@PathVariable Integer id) { UserInfo userInfo = userInfoService.getByName(id); if (userInfo == null) { return '沒有該用戶'; } return userInfo; } @PostMapping('/userInfo') public Object createUserInfo(@RequestBody UserInfo userInfo) { userInfoService.addUserInfo(userInfo); return 'SUCCESS'; } @PutMapping('/userInfo') public Object updateUserInfo(@RequestBody UserInfo userInfo) { UserInfo newUserInfo = userInfoService.updateUserInfo(userInfo); if (newUserInfo == null){ return '不存在該用戶'; } return newUserInfo; } @DeleteMapping('/userInfo/{id}') public Object deleteUserInfo(@PathVariable Integer id) { userInfoService.deleteById(id); return 'SUCCESS'; }}五、SpringBoot 集成 Caffeine 方式二

1、Maven 引入相關(guān)依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <groupId>mydlq.club</groupId> <artifactId>springboot-caffeine-cache-example-2</artifactId> <version>0.0.1</version> <name>springboot-caffeine-cache-example-2</name> <description>Demo project for Spring Boot caffeine</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

2、配置緩存配置類

@Configurationpublic class CacheConfig { /** * 配置緩存管理器 * * @return 緩存管理器 */ @Bean('caffeineCacheManager') public CacheManager cacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine(Caffeine.newBuilder() // 設(shè)置最后一次寫入或訪問后經(jīng)過固定時(shí)間過期 .expireAfterAccess(60, TimeUnit.SECONDS) // 初始的緩存空間大小 .initialCapacity(100) // 緩存的最大條數(shù) .maximumSize(1000)); return cacheManager; }}

3、定義測試的實(shí)體對象

@Data@ToStringpublic class UserInfo { private Integer id; private String name; private String sex; private Integer age;}

4、定義服務(wù)接口類和實(shí)現(xiàn)類

服務(wù)接口

import mydlq.club.example.entity.UserInfo;public interface UserInfoService { /** * 增加用戶信息 * * @param userInfo 用戶信息 */ void addUserInfo(UserInfo userInfo); /** * 獲取用戶信息 * * @param id 用戶ID * @return 用戶信息 */ UserInfo getByName(Integer id); /** * 修改用戶信息 * * @param userInfo 用戶信息 * @return 用戶信息 */ UserInfo updateUserInfo(UserInfo userInfo); /** * 刪除用戶信息 * * @param id 用戶ID */ void deleteById(Integer id);}

服務(wù)實(shí)現(xiàn)類

import lombok.extern.slf4j.Slf4j;import mydlq.club.example.entity.UserInfo;import mydlq.club.example.service.UserInfoService;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import org.springframework.util.StringUtils;import java.util.HashMap;@Slf4j@Service@CacheConfig(cacheNames = 'caffeineCacheManager')public class UserInfoServiceImpl implements UserInfoService { /** * 模擬數(shù)據(jù)庫存儲數(shù)據(jù) */ private HashMap<Integer, UserInfo> userInfoMap = new HashMap<>(); @Override @CachePut(key = '#userInfo.id') public void addUserInfo(UserInfo userInfo) { log.info('create'); userInfoMap.put(userInfo.getId(), userInfo); } @Override @Cacheable(key = '#id') public UserInfo getByName(Integer id) { log.info('get'); return userInfoMap.get(id); } @Override @CachePut(key = '#userInfo.id') public UserInfo updateUserInfo(UserInfo userInfo) { log.info('update'); if (!userInfoMap.containsKey(userInfo.getId())) { return null; } // 取舊的值 UserInfo oldUserInfo = userInfoMap.get(userInfo.getId()); // 替換內(nèi)容 if (!StringUtils.isEmpty(oldUserInfo.getAge())) { oldUserInfo.setAge(userInfo.getAge()); } if (!StringUtils.isEmpty(oldUserInfo.getName())) { oldUserInfo.setName(userInfo.getName()); } if (!StringUtils.isEmpty(oldUserInfo.getSex())) { oldUserInfo.setSex(userInfo.getSex()); } // 將新的對象存儲,更新舊對象信息 userInfoMap.put(oldUserInfo.getId(), oldUserInfo); // 返回新對象信息 return oldUserInfo; } @Override @CacheEvict(key = '#id') public void deleteById(Integer id) { log.info('delete'); userInfoMap.remove(id); }}

5、測試的 Controller 類

import mydlq.club.example.entity.UserInfo;import mydlq.club.example.service.UserInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@RestController@RequestMappingpublic class UserInfoController { @Autowired private UserInfoService userInfoService; @GetMapping('/userInfo/{id}') public Object getUserInfo(@PathVariable Integer id) { UserInfo userInfo = userInfoService.getByName(id); if (userInfo == null) { return '沒有該用戶'; } return userInfo; } @PostMapping('/userInfo') public Object createUserInfo(@RequestBody UserInfo userInfo) { userInfoService.addUserInfo(userInfo); return 'SUCCESS'; } @PutMapping('/userInfo') public Object updateUserInfo(@RequestBody UserInfo userInfo) { UserInfo newUserInfo = userInfoService.updateUserInfo(userInfo); if (newUserInfo == null){ return '不存在該用戶'; } return newUserInfo; } @DeleteMapping('/userInfo/{id}') public Object deleteUserInfo(@PathVariable Integer id) { userInfoService.deleteById(id); return 'SUCCESS'; }}

參考地址:

https://www.jianshu.com/p/c72fb0c787fchttps://www.cnblogs.com/rickiyang/p/11074158.htmlhttps://github.com/my-dlq/blog-example/tree/master/springboot/springboot-caffeine-cache-example

到此這篇關(guān)于Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine的文章就介紹到這了,更多相關(guān)Spring Boot 2.x Caffeine內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
四虎成人av| 国产精品一区二区三区四区在线观看| 日韩视频1区| 蜜臀av国产精品久久久久| 精品日韩视频| 色网在线免费观看| 国产aⅴ精品一区二区四区| 日韩亚洲精品在线| 久久蜜桃精品| 久久精品不卡| 国产专区一区| 亚洲香蕉网站| 亚洲精品99| 久久av一区| 日韩中文字幕一区二区三区| 国产视频一区免费看| 视频一区在线播放| 免费看黄色91| 欧美日韩91| 欧美xxxx中国| 日韩在线免费| 免费日韩av片| 黄色亚洲大片免费在线观看| 一区二区三区四区精品视频| 久久国产乱子精品免费女| 成人在线免费观看网站| 最新日韩欧美| 亚洲精品国模| 国产麻豆一区二区三区| 国产一区一一区高清不卡| 九九色在线视频| 亚洲综合中文| 国产一区一一区高清不卡| av亚洲在线观看| 国产一级成人av| 欧美亚洲精品在线| 欧美日韩亚洲国产精品| 国产欧美另类| 在线日韩中文| 国产精品一区三区在线观看| 亚洲91视频| 国产精品白丝av嫩草影院| 香蕉国产精品| 精品理论电影在线| 亚洲精选91| 日韩欧美一区二区三区在线观看| 综合日韩在线| 亚洲综合在线电影| 午夜电影一区| 国产日韩专区| 日韩不卡在线| 美日韩一区二区三区| 久久激情婷婷| 欧美交a欧美精品喷水| 亚洲一区区二区| 亚洲风情在线资源| 精品久久一区| 国产精品巨作av| 欧美日一区二区三区在线观看国产免| 欧美sm一区| 激情久久一区二区| 青草综合视频| 日韩中文字幕亚洲一区二区va在线| 新版的欧美在线视频| 欧美xxxx中国| 日韩激情视频网站| 日韩不卡在线观看日韩不卡视频| 老牛影视精品| 久久激情五月婷婷| 日韩中文字幕一区二区三区| 欧美中文字幕一区二区| 久久三级视频| 99在线精品视频在线观看| 国产国产精品| 99在线观看免费视频精品观看| 91高清一区| 九一国产精品| 亚洲一区二区三区四区电影 | 亚洲一区国产一区| 在线精品观看| 亚洲人成亚洲精品| 日韩福利在线观看| 国产精品亚洲综合久久| 欧美黄页在线免费观看 | 日本中文字幕视频一区| 亚洲精品裸体| 欧美国产日韩电影| 日韩欧美午夜| 欧美专区18| 亚洲精品黄色| 久久精品一区二区国产| 国产精品一级| 久久久成人网| 欧美专区18| 国产精品www.| 午夜久久一区| 国产精品毛片久久久| 日韩一区二区三区免费播放| 国产亚洲一级| 美女久久久精品| 91高清一区| 国际精品欧美精品| 首页欧美精品中文字幕| 久久久亚洲欧洲日产| 国产一区亚洲| 精品黄色一级片| 在线日韩欧美| 国产精品玖玖玖在线资源| 欧美成人国产| 麻豆国产精品777777在线| 宅男噜噜噜66国产日韩在线观看| 久久免费精品| 日本不卡不码高清免费观看 | 久久精品国产久精国产| 亚洲欧美日韩高清在线| 久久免费视频66| 日韩和欧美的一区| 久久美女精品| 国产videos久久| 最新国产精品视频| 不卡av一区二区| 精品久久精品| 国产精品中文字幕制服诱惑| 在线观看一区| 宅男在线一区| 91精品蜜臀一区二区三区在线| 久久一区视频| 精品一区二区三区免费看| 国产精品欧美一区二区三区不卡| 亚洲人成亚洲精品| 丝袜a∨在线一区二区三区不卡| 欧美成人日韩| 9国产精品视频| 欧美专区一区二区三区| 免播放器亚洲| 日韩影院二区| 日韩av首页| 欧美aa在线观看| 黑人精品一区| 成人日韩在线观看| 亚洲女同一区| 蜜桃视频一区二区| 国产色99精品9i| 久久久久观看| 日韩av福利| 国产亚洲高清视频| 欧美精品国产白浆久久久久| 麻豆国产一区| 亚洲不卡av不卡一区二区| 免费不卡中文字幕在线| 一区二区三区四区日本视频| 成人啊v在线| 亚洲色图网站| 精品中国亚洲| 99成人在线视频| 蜜臀精品久久久久久蜜臀| 欧美亚洲福利| 欧美.日韩.国产.一区.二区| 视频一区二区三区在线| 国产日本精品| 久久久久午夜电影| 日韩精品成人| 日韩在线高清| 亚洲综合色婷婷在线观看| 久久精品一区二区国产| 水蜜桃久久夜色精品一区| 老司机精品久久| 成人精品久久| 亚洲精品高潮| 欧美 日韩 国产一区二区在线视频 | 欧美影院视频| 日本精品影院| 日韩精品久久理论片| 久久精品国产999大香线蕉| 1024精品久久久久久久久| 欧美日本一区| 成人久久一区| 欧美aⅴ一区二区三区视频| 蜜臀av性久久久久蜜臀aⅴ四虎| 国产91欧美| 国产欧美精品久久| 亚洲一区有码| 激情五月综合网| 精品视频99| 国产精品天堂蜜av在线播放| 丝袜脚交一区二区| 亚洲婷婷免费| 成人羞羞视频播放网站| 亚洲精品在线国产| 久久aⅴ国产紧身牛仔裤| 欧美aa在线观看| 国产一区二区三区四区大秀| 日本国产欧美| 亚洲日产国产精品| 亚洲综合中文| 男人的天堂亚洲一区| 性欧美69xoxoxoxo| 在线一区免费| 99国产精品| 久久xxxx精品视频|