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

SpringBoot實(shí)現(xiàn)Read Through模式的操作過程

 更新時(shí)間:2024年07月08日 15:06:29   作者:菜鳥翻身做主人  
Read Through模式通常是指一種緩存策略,其中當(dāng)應(yīng)用程序嘗試讀取數(shù)據(jù)時(shí),緩存系統(tǒng)首先被檢查以查看數(shù)據(jù)是否已經(jīng)存在于緩存中,這篇文章主要介紹了SpringBoot實(shí)現(xiàn)Read Through模式,需要的朋友可以參考下

簡介

Read Through模式通常是指一種緩存策略,其中當(dāng)應(yīng)用程序嘗試讀取數(shù)據(jù)時(shí),緩存系統(tǒng)首先被檢查以查看數(shù)據(jù)是否已經(jīng)存在于緩存中。如果緩存中存在數(shù)據(jù)(即緩存命中),則直接從緩存中讀取數(shù)據(jù)并返回給應(yīng)用程序。如果緩存中不存在數(shù)據(jù)(即緩存未命中),則從底層的數(shù)據(jù)存儲(chǔ)(如數(shù)據(jù)庫)中讀取數(shù)據(jù),然后將數(shù)據(jù)加載到緩存中,最后再返回給應(yīng)用程序。

這種模式的主要優(yōu)點(diǎn)包括:

  • 提高性能:通過減少對底層存儲(chǔ)的直接訪問次數(shù),可以顯著提高數(shù)據(jù)檢索的性能。
  • 減少延遲:緩存通常位于內(nèi)存中,訪問速度比磁盤存儲(chǔ)快得多,因此可以減少數(shù)據(jù)檢索的延遲。
  • 減輕數(shù)據(jù)庫負(fù)載:通過在緩存中存儲(chǔ)頻繁訪問的數(shù)據(jù),可以減少對數(shù)據(jù)庫的查詢壓力,從而提高整個(gè)系統(tǒng)的吞吐量。

Read Through模式通常與Lazy Loading(懶加載)和Eager Loading(急加載)等策略相對比:

  • Lazy Loading:數(shù)據(jù)僅在需要時(shí)才加載,這可以減少不必要的數(shù)據(jù)加載,但可能會(huì)增加首次訪問的延遲。
  • Eager Loading:預(yù)先加載數(shù)據(jù),這可以減少首次訪問的延遲,但可能會(huì)增加應(yīng)用程序的內(nèi)存使用和啟動(dòng)時(shí)間。

在實(shí)現(xiàn)Read Through模式時(shí),可能需要考慮以下方面:

  • 緩存失效策略:確定何時(shí)從緩存中移除數(shù)據(jù),例如基于時(shí)間(TTL)或基于空間(當(dāng)緩存達(dá)到一定大小時(shí))。
  • 并發(fā)控制:處理多個(gè)應(yīng)用程序?qū)嵗瑫r(shí)訪問和修改緩存的情況。
  • 數(shù)據(jù)一致性:確保緩存中的數(shù)據(jù)與底層存儲(chǔ)中的數(shù)據(jù)保持一致,特別是在數(shù)據(jù)更新時(shí)。

實(shí)現(xiàn)

在Spring Boot中實(shí)現(xiàn)Read Through模式,通??梢酝ㄟ^Spring Cache抽象來完成。Spring Cache提供了一個(gè)跨不同緩存實(shí)現(xiàn)的統(tǒng)一API,并且支持多種緩存解決方案,如EhCache、Hazelcast、Infinispan、Redis等。

添加依賴:首先,需要添加Spring Boot的緩存依賴和選擇的緩存實(shí)現(xiàn)庫(如Redis)

<!-- Spring Boot Starter Cache -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 以Redis為例,添加Redis的Spring Boot Starter -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

啟用緩存注解:在Spring Boot的配置類上添加@EnableCaching注解,以啟用緩存注解支持。

配置緩存管理器:配置一個(gè)或多個(gè)CacheManager,Spring Boot會(huì)自動(dòng)配置一個(gè)簡單的CacheManager,但你可以根據(jù)需要配置更復(fù)雜的緩存策略。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@Configuration
public class RedisCacheConfig {
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer.json())))
Map<String, RedisCacheConfiguration> customCacheConfigs = new HashMap<>();
customCacheConfigs.put("mySpecialCache", 
    config.entryTtl(Duration.ofMinutes(15))); // 為特定緩存設(shè)置不同的過期時(shí)間
                .disableCachingNullValues();
        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
            // 在這里可以自定義添加緩存配置
                .withInitialCacheConfigurations(customCacheConfigs)
                .build();
    }
}

使用緩存注解:在需要緩存的方法上使用@Cacheable注解來實(shí)現(xiàn)Read Through模式。如果緩存中沒有數(shù)據(jù),方法將被調(diào)用,結(jié)果將被緩存。

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class MyService {
    @Cacheable(value = "myCache", key = "#id")
    public MyData getDataById(String id) {
        // 從數(shù)據(jù)庫加載數(shù)據(jù)
        return myDataRepository.findById(id);
    }
}

緩存鍵值:在@Cacheable注解中指定緩存的鍵值,這通常是基于方法參數(shù)的值。

緩存名稱:指定緩存名稱,這將用于區(qū)分不同的緩存域。

配置緩存參數(shù):可以根據(jù)需要配置緩存的超時(shí)時(shí)間、條件、除非條件等

value或cacheNames:指定緩存名稱。可以指定一個(gè)或多個(gè)緩存名稱,它們將用于存儲(chǔ)緩存。

@Cacheable(value = "myCacheName", key = "#id")

key:定義緩存鍵值的生成策略。通常使用SpEL表達(dá)式(Spring Expression Language)來指定方法參數(shù)作為緩存鍵。

@Cacheable(cacheNames = "myCache", key = "#id")

condition:定義緩存的條件,只有滿足條件時(shí)才進(jìn)行緩存。

@Cacheable(cacheNames = "myCache", key = "#id", condition = "#id.length() > 3")

unless:定義不進(jìn)行緩存的條件,與condition相反,用于排除某些情況。

@Cacheable(cacheNames = "myCache", key = "#id", unless = "#result == null")

keyGenerator:指定自定義的緩存鍵生成策略,如果需要更復(fù)雜的鍵生成邏輯,可以指定一個(gè)KeyGenerator的Bean名稱。

@Cacheable(cacheNames = "myCache", keyGenerator = "myKeyGenerator")

cacheManager:指定使用哪個(gè)CacheManager,如果有多個(gè)CacheManager時(shí)使用。

@Cacheable(cacheNames = "myCache", cacheManager = "myCacheManager")

expireAfterWrite:設(shè)置緩存項(xiàng)寫入后過期時(shí)間(單位為毫秒)。這是一種常用的配置,用于定義緩存數(shù)據(jù)的生存時(shí)間。

@Cacheable(cacheNames = "myCache", key = "#id", expireAfterWrite = 3600000) // 1小時(shí)后過期

expireAfterAccess:設(shè)置緩存項(xiàng)最后一次訪問后過期時(shí)間,適用于緩存數(shù)據(jù)在最后一次被訪問后多久過期。

refreshAfterWrite:設(shè)置寫入后多久刷新緩存,適用于動(dòng)態(tài)刷新緩存的場景。

sync:設(shè)置是否同步創(chuàng)建緩存項(xiàng),防止并發(fā)環(huán)境下的競態(tài)條件。

異常處理:確保處理緩存方法中可能拋出的異常,以避免影響應(yīng)用程序的穩(wěn)定性。

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)Read Through模式的文章就介紹到這了,更多相關(guān)SpringBoot Read Through模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論