欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成本地緩存性能之王Caffeine示例詳解

 更新時(shí)間:2022年07月22日 11:44:48   作者:碼哥字節(jié)  
這篇文章主要為大家介紹了SpringBoot集成本地緩存性能之王Caffeine的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

使用緩存的目的就是提高性能,今天碼哥帶大家實(shí)踐運(yùn)用 spring-boot-starter-cache 抽象的緩存組件去集成本地緩存性能之王 Caffeine。

大家需要注意的是:in-memeory 緩存只適合在單體應(yīng)用,不適合與分布式環(huán)境。

分布式環(huán)境的情況下需要將緩存修改同步到每個(gè)節(jié)點(diǎn),需要一個(gè)同步機(jī)制保證每個(gè)節(jié)點(diǎn)緩存數(shù)據(jù)最終一致。

Spring Cache 是什么

不使用 Spring Cache 抽象的緩存接口,我們需要根據(jù)不同的緩存框架去實(shí)現(xiàn)緩存,需要在對(duì)應(yīng)的代碼里面去對(duì)應(yīng)緩存加載、刪除、更新等。

比如查詢(xún)我們使用旁路緩存策略:先從緩存中查詢(xún)數(shù)據(jù),如果查不到則從數(shù)據(jù)庫(kù)查詢(xún)并寫(xiě)到緩存中。

偽代碼如下:

public User getUser(long userId) {
    // 從緩存查詢(xún)
    User user = cache.get(userId);
    if (user != null) {
        return user;
    }
    // 從數(shù)據(jù)庫(kù)加載
    User dbUser = loadDataFromDB(userId);
    if (dbUser != null) {
        // 設(shè)置到緩存中
        cache.put(userId, dbUser)
    }
    return dbUser;
}

我們需要寫(xiě)大量的這種繁瑣代碼,Spring Cache 則對(duì)緩存進(jìn)行了抽象,提供了如下幾個(gè)注解實(shí)現(xiàn)了緩存管理:

  • @Cacheable:觸發(fā)緩存讀取操作,用于查詢(xún)方法上,如果緩存中找到則直接取出緩存并返回,否則執(zhí)行目標(biāo)方法并將結(jié)果緩存。
  • @CachePut:觸發(fā)緩存更新的方法上,與 Cacheable 相比,該注解的方法始終都會(huì)被執(zhí)行,并且使用方法返回的結(jié)果去更新緩存,適用于 insert 和 update 行為的方法上。
  • @CacheEvict:觸發(fā)緩存失效,刪除緩存項(xiàng)或者清空緩存,適用于 delete 方法上。

除此之外,抽象的 CacheManager 既能集成基于本地內(nèi)存的單體應(yīng)用,也能集成 EhCache、Redis 等緩存服務(wù)器。

最方便的是通過(guò)一些簡(jiǎn)單配置和注解就能接入不同的緩存框架,無(wú)需修改任何代碼。

集成 Caffeine

碼哥帶大家使用注解方式完成緩存操作的方式來(lái)集成,完整的代碼請(qǐng)?jiān)L問(wèn) github:在 pom.xml 文件添加如下依賴(lài):

<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>

使用 JavaConfig 方式配置 CacheManager:

@Slf4j
@EnableCaching
@Configuration
public class CacheConfig {
    @Autowired
    @Qualifier("cacheExecutor")
    private Executor cacheExecutor;
    @Bean
    public Caffeine<Object, Object> caffeineCache() {
        return Caffeine.newBuilder()
                // 設(shè)置最后一次寫(xiě)入或訪問(wèn)后經(jīng)過(guò)固定時(shí)間過(guò)期
                .expireAfterAccess(7, TimeUnit.DAYS)
                // 初始的緩存空間大小
                .initialCapacity(500)
                // 使用自定義線程池
                .executor(cacheExecutor)
                .removalListener(((key, value, cause) -> log.info("key:{} removed, removalCause:{}.", key, cause.name())))
                // 緩存的最大條數(shù)
                .maximumSize(1000);
    }
    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        caffeineCacheManager.setCaffeine(caffeineCache());
        // 不緩存空值
        caffeineCacheManager.setAllowNullValues(false);
        return caffeineCacheManager;
    }
}

準(zhǔn)備工作搞定,接下來(lái)就是如何使用了。

@Slf4j
@Service
public class AddressService {
    public static final String CACHE_NAME = "caffeine:address";
    private static final AtomicLong ID_CREATOR = new AtomicLong(0);
    private Map&lt;Long, AddressDTO&gt; addressMap;
    public AddressService() {
        addressMap = new ConcurrentHashMap&lt;&gt;();
        addressMap.put(ID_CREATOR.incrementAndGet(), AddressDTO.builder().customerId(ID_CREATOR.get()).address("地址1").build());
        addressMap.put(ID_CREATOR.incrementAndGet(), AddressDTO.builder().customerId(ID_CREATOR.get()).address("地址2").build());
        addressMap.put(ID_CREATOR.incrementAndGet(), AddressDTO.builder().customerId(ID_CREATOR.get()).address("地址3").build());
    }
    @Cacheable(cacheNames = {CACHE_NAME}, key = "#customerId")
    public AddressDTO getAddress(long customerId) {
        log.info("customerId:{} 沒(méi)有走緩存,開(kāi)始從數(shù)據(jù)庫(kù)查詢(xún)", customerId);
        return addressMap.get(customerId);
    }
    @CachePut(cacheNames = {CACHE_NAME}, key = "#result.customerId")
    public AddressDTO create(String address) {
        long customerId = ID_CREATOR.incrementAndGet();
        AddressDTO addressDTO = AddressDTO.builder().customerId(customerId).address(address).build();
        addressMap.put(customerId, addressDTO);
        return addressDTO;
    }
    @CachePut(cacheNames = {CACHE_NAME}, key = "#result.customerId")
    public AddressDTO update(Long customerId, String address) {
        AddressDTO addressDTO = addressMap.get(customerId);
        if (addressDTO == null) {
            throw new RuntimeException("沒(méi)有 customerId = " + customerId + "的地址");
        }
        addressDTO.setAddress(address);
        return addressDTO;
    }
    @CacheEvict(cacheNames = {CACHE_NAME}, key = "#customerId")
    public boolean delete(long customerId) {
        log.info("緩存 {} 被刪除", customerId);
        return true;
    }
}

使用 CacheName 隔離不同業(yè)務(wù)場(chǎng)景的緩存,每個(gè) Cache 內(nèi)部持有一個(gè) map 結(jié)構(gòu)存儲(chǔ)數(shù)據(jù),key 可用使用 Spring 的 Spel 表達(dá)式。

單元測(cè)試走起:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CaffeineApplication.class)
@Slf4j
public class CaffeineApplicationTests {
    @Autowired
    private AddressService addressService;
    @Autowired
    private CacheManager cacheManager;
    @Test
    public void testCache() {
        // 插入緩存 和數(shù)據(jù)庫(kù)
        AddressDTO newInsert = addressService.create("南山大道");
        // 要走緩存
        AddressDTO address = addressService.getAddress(newInsert.getCustomerId());
        long customerId = 2;
        // 第一次未命中緩存,打印 customerId:{} 沒(méi)有走緩存,開(kāi)始從數(shù)據(jù)庫(kù)查詢(xún)
        AddressDTO address2 = addressService.getAddress(customerId);
        // 命中緩存
        AddressDTO cacheAddress2 = addressService.getAddress(customerId);
        // 更新數(shù)據(jù)庫(kù)和緩存
        addressService.update(customerId, "地址 2 被修改");
        // 更新后查詢(xún),依然命中緩存
        AddressDTO hitCache2 = addressService.getAddress(customerId);
        Assert.assertEquals(hitCache2.getAddress(), "地址 2 被修改");
        // 刪除緩存
        addressService.delete(customerId);
        // 未命中緩存, 從數(shù)據(jù)庫(kù)讀取
        AddressDTO hit = addressService.getAddress(customerId);
        System.out.println(hit.getCustomerId());
    }
}

大家發(fā)現(xiàn)沒(méi),只需要在對(duì)應(yīng)的方法上加上注解,就能愉快的使用緩存了。需要注意的是, 設(shè)置的 cacheNames 一定要對(duì)應(yīng),每個(gè)業(yè)務(wù)場(chǎng)景使用對(duì)應(yīng)的 cacheNames。

另外 key 可以使用 spel 表達(dá)式,大家重點(diǎn)可以關(guān)注 @CachePut(cacheNames = {CACHE_NAME}, key = "#result.customerId"),result 表示接口返回結(jié)果,Spring 提供了幾個(gè)元數(shù)據(jù)直接使用。

名稱(chēng)地點(diǎn)描述例子
methodName根對(duì)象被調(diào)用的方法的名稱(chēng)#root.methodName
method根對(duì)象被調(diào)用的方法#root.method.name
target根對(duì)象被調(diào)用的目標(biāo)對(duì)象#root.target
targetClass根對(duì)象被調(diào)用的目標(biāo)的類(lèi)#root.targetClass
args根對(duì)象用于調(diào)用目標(biāo)的參數(shù)(作為數(shù)組)#root.args[0]
caches根對(duì)象運(yùn)行當(dāng)前方法的緩存集合#root.caches[0].name
參數(shù)名稱(chēng)評(píng)估上下文任何方法參數(shù)的名稱(chēng)。如果名稱(chēng)不可用(可能是由于沒(méi)有調(diào)試信息),則參數(shù)名稱(chēng)也可在#a<#arg> where#arg代表參數(shù)索引(從 開(kāi)始0)下獲得。#iban或#a0(您也可以使用#p0或#p<#arg>表示法作為別名)。
result評(píng)估上下文方法調(diào)用的結(jié)果(要緩存的值)。僅在unless 表達(dá)式、cache put表達(dá)式(計(jì)算key)或cache evict 表達(dá)式(when beforeInvocationis false)中可用。對(duì)于支持的包裝器(例如 Optional),#result指的是實(shí)際對(duì)象,而不是包裝器。#result

核心原理

Java Caching定義了5個(gè)核心接口,分別是 CachingProvider, CacheManager, Cache, Entry 和 Expiry。

核心類(lèi)圖:

  • Cache:抽象了緩存的操作,比如,get()、put();
  • CacheManager:管理 Cache,可以理解成 Cache 的集合管理,之所以有多個(gè) Cache,是因?yàn)榭梢愿鶕?jù)不同場(chǎng)景使用不同的緩存失效時(shí)間和數(shù)量限制。
  • CacheInterceptor、CacheAspectSupport、AbstractCacheInvoker:CacheInterceptor 是一個(gè)AOP 方法攔截器,在方法前后做額外的邏輯,比如查詢(xún)操作,先查緩存,找不到數(shù)據(jù)再執(zhí)行方法,并把方法的結(jié)果寫(xiě)入緩存等,它繼承了CacheAspectSupport(緩存操作的主體邏輯)、AbstractCacheInvoker(封裝了對(duì) Cache 的讀寫(xiě))。
  • CacheOperation、AnnotationCacheOperationSource、SpringCacheAnnotationParser:CacheOperation定義了緩存操作的緩存名字、緩存key、緩存條件condition、CacheManager等,AnnotationCacheOperationSource 是一個(gè)獲取緩存注解對(duì)應(yīng) CacheOperation 的類(lèi),而SpringCacheAnnotationParser 是解析注解的類(lèi),解析后會(huì)封裝成 CacheOperation 集合供AnnotationCacheOperationSource 查找。

CacheAspectSupport:緩存切面支持類(lèi),是CacheInterceptor 的父類(lèi),封裝了所有的緩存操作的主體邏輯。

主要流程如下:

  • 通過(guò)CacheOperationSource,獲取所有的CacheOperation列表
  • 如果有@CacheEvict注解、并且標(biāo)記為在調(diào)用前執(zhí)行,則做刪除/清空緩存的操作
  • 如果有@Cacheable注解,查詢(xún)緩存
  • 如果緩存未命中(查詢(xún)結(jié)果為null),則新增到cachePutRequests,后續(xù)執(zhí)行原始方法后會(huì)寫(xiě)入緩存
  • 緩存命中時(shí),使用緩存值作為結(jié)果;緩存未命中、或有@CachePut注解時(shí),需要調(diào)用原始方法,使用原始方法的返回值作為結(jié)果
  • 如果有@CachePut注解,則新增到cachePutRequests
  • 如果緩存未命中,則把查詢(xún)結(jié)果值寫(xiě)入緩存;如果有@CachePut注解,也把方法執(zhí)行結(jié)果寫(xiě)入緩存
  • 如果有@CacheEvict注解、并且標(biāo)記為在調(diào)用后執(zhí)行,則做刪除/清空緩存的操作

今天就到這了,分享一些工作小技巧給大家,后面碼哥會(huì)分享如何接入 Redis ,并且?guī)Т蠹覍?shí)現(xiàn)一個(gè)基于 Sping Boot 實(shí)現(xiàn)一個(gè) Caffeine 作為一級(jí)緩存、Redis 作為二級(jí)緩存的分布式二級(jí)緩存框架。

我們下期見(jiàn),大家可以在評(píng)論區(qū)叫我靚仔么?不叫也行,點(diǎn)贊分享也是鼓勵(lì)。

參考資料

[1]http://www.dbjr.com.cn/article/242800.htm

[2]https://docs.spring.io/spring...

以上就是SpringBoot集成本地緩存性能之王Caffeine示例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成Caffeine的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺談Springboot下引入mybatis遇到的坑點(diǎn)

    淺談Springboot下引入mybatis遇到的坑點(diǎn)

    這篇文章主要介紹了Springboot下引入mybatis遇到的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java之如何關(guān)閉流

    Java之如何關(guān)閉流

    這篇文章主要介紹了Java之如何關(guān)閉流問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • java實(shí)現(xiàn)對(duì)對(duì)碰小游戲

    java實(shí)現(xiàn)對(duì)對(duì)碰小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)對(duì)對(duì)碰小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • java Long類(lèi)型轉(zhuǎn)為String類(lèi)型的兩種方式及區(qū)別說(shuō)明

    java Long類(lèi)型轉(zhuǎn)為String類(lèi)型的兩種方式及區(qū)別說(shuō)明

    這篇文章主要介紹了java Long類(lèi)型轉(zhuǎn)為String類(lèi)型的兩種方式及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java字符串相加時(shí)的內(nèi)存表現(xiàn)和原理分析

    java字符串相加時(shí)的內(nèi)存表現(xiàn)和原理分析

    這篇文章主要介紹了java字符串相加時(shí)的內(nèi)存表現(xiàn)和原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java 動(dòng)態(tài)代理的方法總結(jié)

    java 動(dòng)態(tài)代理的方法總結(jié)

    這篇文章主要介紹了java 動(dòng)態(tài)代理的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • springboot2.x引入feign踩的坑及解決

    springboot2.x引入feign踩的坑及解決

    這篇文章主要介紹了springboot2.x引入feign踩的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 淺析SpringBoot中的過(guò)濾器和攔截器

    淺析SpringBoot中的過(guò)濾器和攔截器

    過(guò)濾器和攔截器都是為了在請(qǐng)求到達(dá)目標(biāo)處理器(Servlet或Controller)之前或者之后插入自定義的處理邏輯,下面就跟隨小編來(lái)看看它們二者的區(qū)別和具體使用吧
    2024-03-03
  • Java的MyBatis快速入門(mén)和實(shí)戰(zhàn)詳解

    Java的MyBatis快速入門(mén)和實(shí)戰(zhàn)詳解

    這篇文章主要介紹了Java的MyBatis快速入門(mén)和實(shí)戰(zhàn)詳解,MyBatis是一款優(yōu)秀的持久層框架,用于簡(jiǎn)化JDBC開(kāi)發(fā),是一套可重用的,通用的,軟件基礎(chǔ)代碼模型,需要的朋友可以參考下
    2023-05-05
  • 利用spring-data-redis實(shí)現(xiàn)incr自增的操作

    利用spring-data-redis實(shí)現(xiàn)incr自增的操作

    這篇文章主要介紹了利用spring-data-redis實(shí)現(xiàn)incr自增的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11

最新評(píng)論