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

Redis緩存工具封裝實現(xiàn)

 更新時間:2023年01月15日 11:02:01   作者:嗯mua.  
本文主要介紹了Redis緩存工具封裝實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

將 StringRedisTemplate 封裝成一個緩存工具類,方便以后重復(fù)使用。

1. 方法要求

在這個工具類中我們完成四個方法:

  • 方法①:將任意Java對象序列化為json并存儲在string類型的key中,并且可以設(shè)置TTL過期時間
  • 方法②:將任意Java對象序列化為json并存儲在string類型的key中,并且可以設(shè)置邏輯過期時間,用于處理緩存擊穿問題
  • 方法③:根據(jù)指定的key查詢緩存,并反序列化為指定類型,利用緩存空值的方式解決緩存穿透問題
  • 方法④:根據(jù)指定的key查詢緩存,并反序列化為指定類型,需要利用邏輯過期解決緩存擊穿問題

我們新建一個類,先把大致框架寫出來,方法的參數(shù)可以邊寫邊完善,但是我的方法參數(shù)已經(jīng)完善好了:

@Component
public class CacheClient {

? ? private final StringRedisTemplate stringRedisTemplate;

? ? public CacheClient(StringRedisTemplate stringRedisTemplate) {
? ? ? ? this.stringRedisTemplate = stringRedisTemplate;
? ? }

? ? //方法一
? ? public void set(String key, Object value, Long time, TimeUnit unit) {
? ? }

? ? //方法二
? ? public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {
? ? }

? ? //方法三
? ? public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Long time, TimeUnit unit, Function<ID, R> dbFallback) {
? ? }

? ? //方法四
? ? public <R, ID> R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class<R> type,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Long time, TimeUnit unit, Function<ID, R> dbFallback) {
? ? }

? ? //線程池
? ? private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);

? ? //獲取鎖
? ? private boolean tryLock(String key) {
? ? ? ? Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", LOCK_SHOP_TTL, TimeUnit.SECONDS);
? ? ? ? return BooleanUtil.isTrue(flag);
? ? }

? ? //釋放鎖
? ? private void unLock(String key) {
? ? ? ? stringRedisTemplate.delete(key);
? ? }
}

接下來我們可以不斷完善這些方法。

1.1 方法一

public void set(String key, Object value, Long time, TimeUnit unit) {
? ? stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);
}

1.2 方法二

public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {
? ? RedisData redisData = new RedisData();
? ? redisData.setData(value);
? ? redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
? ? stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
}

1.3 方法三

public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type,
                                          Long time, TimeUnit unit, Function<ID, R> dbFallback) {
    String key = keyPrefix + id;
    //1.從redis中查詢商鋪緩存
    String json = stringRedisTemplate.opsForValue().get(key);
    //2.判斷是否存在
    if (StrUtil.isNotBlank(json)) {
        //2.1.存在
        return JSONUtil.toBean(json, type);
    }
    //2.2.不存在
    //判斷是否為空值
    if (json != null) {
        //不為null,則必為空
        return null;
    }
    //3.查詢數(shù)據(jù)庫
    R r = dbFallback.apply(id);
    if (r == null) {
        //3.1.不存在,緩存空值
        stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);
    } else {
        //3.2.存在,緩存數(shù)據(jù)
        this.set(key, r, time, unit);
    }
    return r;
}

方法三用到了函數(shù)式編程,這里非常巧妙,順便再貼一下調(diào)用方法是怎樣調(diào)用的:

Shop shop = cacheClient.queryWithPassThrough(CACHE_SHOP_KEY,id,Shop.class,CACHE_SHOP_TTL,TimeUnit.MINUTES,this::getById);

1.4 方法四

public <R, ID> R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class<R> type,
                                            Long time, TimeUnit unit, Function<ID, R> dbFallback) {
    //1.從redis查詢商鋪緩存
    String key = prefix + id;
    String json = stringRedisTemplate.opsForValue().get(key);
    //2.判斷是否存在
    if (StrUtil.isBlank(json)) {
        //未命中,直接返回空
        return null;
    }
    //3.命中,判斷是否過期
    RedisData redisData = JSONUtil.toBean(json, RedisData.class);
    R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
    if (redisData.getExpireTime().isAfter(LocalDateTime.now())) {
        //3.1未過期,直接返回店鋪信息
        return r;
    }
    //3.2.已過期,緩存重建
    //3.3.獲取鎖
    String lockKey = lockPre + id;
    boolean flag = tryLock(lockKey);
    if (flag) {
        //3.4.獲取成功
        //4再次檢查redis緩存是否過期,做double check
        json = stringRedisTemplate.opsForValue().get(key);
        //4.1.判斷是否存在
        if (StrUtil.isBlank(json)) {
            //未命中,直接返回空
            return null;
        }
        //4.2.命中,判斷是否過期
        redisData = JSONUtil.toBean(json, RedisData.class);
        r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
        if (redisData.getExpireTime().isAfter(LocalDateTime.now())) {
            //4.3.未過期,直接返回店鋪信息
            return r;
        }
        //4.4過期,返回舊數(shù)據(jù)
        CACHE_REBUILD_EXECUTOR.submit(() -> {
            //5.重建緩存
            try {
                R r1 = dbFallback.apply(id);
                this.setWithLogicExpire(key, r1, time, unit);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                //釋放鎖
                unLock(lockKey);
            }
        });
    }
    //7.獲取失敗,返回舊數(shù)據(jù)
    return r;
}

2. 完整工具類代碼

@Component
@Slf4j
public class CacheClient {

? ? private final StringRedisTemplate stringRedisTemplate;

? ? public CacheClient(StringRedisTemplate stringRedisTemplate) {
? ? ? ? this.stringRedisTemplate = stringRedisTemplate;
? ? }

? ? public void set(String key, Object value, Long time, TimeUnit unit) {
? ? ? ? stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);
? ? }

? ? public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {
? ? ? ? RedisData redisData = new RedisData();
? ? ? ? redisData.setData(value);
? ? ? ? redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
? ? ? ? stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
? ? }

? ? public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Long time, TimeUnit unit, Function<ID, R> dbFallback) {
? ? ? ? String key = keyPrefix + id;
? ? ? ? //1.從redis中查詢商鋪緩存
? ? ? ? String json = stringRedisTemplate.opsForValue().get(key);
? ? ? ? //2.判斷是否存在
? ? ? ? if (StrUtil.isNotBlank(json)) {
? ? ? ? ? ? //2.1.存在
? ? ? ? ? ? return JSONUtil.toBean(json, type);
? ? ? ? }
? ? ? ? //2.2.不存在
? ? ? ? //判斷是否為空值
? ? ? ? if (json != null) {
? ? ? ? ? ? //不為null,則必為空
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? //3.查詢數(shù)據(jù)庫
? ? ? ? R r = dbFallback.apply(id);
? ? ? ? if (r == null) {
? ? ? ? ? ? //3.1.不存在,緩存空值
? ? ? ? ? ? stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);
? ? ? ? } else {
? ? ? ? ? ? //3.2.存在,緩存數(shù)據(jù)
? ? ? ? ? ? this.set(key, r, time, unit);
? ? ? ? }
? ? ? ? return r;
? ? }

? ? public <R, ID> R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class<R> type,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Long time, TimeUnit unit, Function<ID, R> dbFallback) {
? ? ? ? //1.從redis查詢商鋪緩存
? ? ? ? String key = prefix + id;
? ? ? ? String json = stringRedisTemplate.opsForValue().get(key);
? ? ? ? //2.判斷是否存在
? ? ? ? if (StrUtil.isBlank(json)) {
? ? ? ? ? ? //未命中,直接返回空
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? //3.命中,判斷是否過期
? ? ? ? RedisData redisData = JSONUtil.toBean(json, RedisData.class);
? ? ? ? R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
? ? ? ? if (redisData.getExpireTime().isAfter(LocalDateTime.now())) {
? ? ? ? ? ? //3.1未過期,直接返回店鋪信息
? ? ? ? ? ? return r;
? ? ? ? }
? ? ? ? //3.2.已過期,緩存重建
? ? ? ? //3.3.獲取鎖
? ? ? ? String lockKey = lockPre + id;
? ? ? ? boolean flag = tryLock(lockKey);
? ? ? ? if (flag) {
? ? ? ? ? ? //3.4.獲取成功
? ? ? ? ? ? //4再次檢查redis緩存是否過期,做double check
? ? ? ? ? ? json = stringRedisTemplate.opsForValue().get(key);
? ? ? ? ? ? //4.1.判斷是否存在
? ? ? ? ? ? if (StrUtil.isBlank(json)) {
? ? ? ? ? ? ? ? //未命中,直接返回空
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? //4.2.命中,判斷是否過期
? ? ? ? ? ? redisData = JSONUtil.toBean(json, RedisData.class);
? ? ? ? ? ? r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
? ? ? ? ? ? if (redisData.getExpireTime().isAfter(LocalDateTime.now())) {
? ? ? ? ? ? ? ? //4.3.未過期,直接返回店鋪信息
? ? ? ? ? ? ? ? return r;
? ? ? ? ? ? }
? ? ? ? ? ? //4.4過期,返回舊數(shù)據(jù)
? ? ? ? ? ? CACHE_REBUILD_EXECUTOR.submit(() -> {
? ? ? ? ? ? ? ? //5.重建緩存
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? R r1 = dbFallback.apply(id);
? ? ? ? ? ? ? ? ? ? this.setWithLogicExpire(key, r1, time, unit);
? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? //釋放鎖
? ? ? ? ? ? ? ? ? ? unLock(lockKey);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? }
? ? ? ? //7.獲取失敗,返回舊數(shù)據(jù)
? ? ? ? return r;
? ? }

? ? private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);

? ? //獲取鎖
? ? private boolean tryLock(String key) {
? ? ? ? Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", LOCK_SHOP_TTL, TimeUnit.SECONDS);
? ? ? ? return BooleanUtil.isTrue(flag);
? ? }

? ? //釋放鎖
? ? private void unLock(String key) {
? ? ? ? stringRedisTemplate.delete(key);
? ? }
}

到此這篇關(guān)于Redis緩存工具封裝實現(xiàn)的文章就介紹到這了,更多相關(guān)Redis緩存工具封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • redis中事務(wù)機制及樂觀鎖的實現(xiàn)

    redis中事務(wù)機制及樂觀鎖的實現(xiàn)

    這篇文章主要介紹了redis中事務(wù)機制及樂觀鎖的相關(guān)內(nèi)容,通過事務(wù)的執(zhí)行分析Redis樂觀鎖,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • Window下對Redis進(jìn)行開啟與關(guān)閉的操作方法

    Window下對Redis進(jìn)行開啟與關(guān)閉的操作方法

    這篇文章主要介紹了Window下對Redis進(jìn)行開啟與關(guān)閉的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-11-11
  • SpringBoot 集成Redis 過程

    SpringBoot 集成Redis 過程

    redis是一個開源的、使用C語言編寫的、支持網(wǎng)絡(luò)交互的、可基于內(nèi)存也可持久化的Key-Value數(shù)據(jù)庫。本文給大家介紹SpringBoot 集成Redis 過程,感興趣的朋友一起看看吧
    2021-06-06
  • Windows系統(tǒng)安裝redis數(shù)據(jù)庫

    Windows系統(tǒng)安裝redis數(shù)據(jù)庫

    這篇文章介紹了Windows系統(tǒng)安裝redis數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • MyBatis緩存和二級緩存整合Redis的解決方案

    MyBatis緩存和二級緩存整合Redis的解決方案

    這篇文章主要介紹了MyBatis緩存和二級緩存整合Redis,將MyBatis緩存和二級緩存整合Redis,可以提高查詢效率,同時也能保證數(shù)據(jù)的可靠性和一致性,需要的朋友可以參考下
    2023-07-07
  • Redis分布式緩存的安裝

    Redis分布式緩存的安裝

    這篇文章主要介紹了Redis分布式緩存的安裝,Redis?是一個開源(BSD許可)的,內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)存儲系統(tǒng),它可以用作數(shù)據(jù)庫、緩存和消息中間件,需要的朋友可以參考下
    2023-05-05
  • redis 主從備份及其主備切換的操作

    redis 主從備份及其主備切換的操作

    這篇文章主要介紹了redis 主從備份及其主備切換的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • redis主從+哨兵搭建的實現(xiàn)示例

    redis主從+哨兵搭建的實現(xiàn)示例

    本文主要介紹了redis主從+哨兵搭建的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • 基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)

    基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)

    基于 spring aop 常規(guī)應(yīng)用場景多是用于日志記錄以及實現(xiàn) redis 分布式鎖,在 github 中也有項目是把它拿來當(dāng)作緩存的異常捕捉,這篇文章主要介紹了基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪,需要的朋友可以參考下
    2022-08-08
  • 利用redis實現(xiàn)聊天記錄轉(zhuǎn)存功能的全過程

    利用redis實現(xiàn)聊天記錄轉(zhuǎn)存功能的全過程

    社交類軟件聊天功能必不可少,聊天記錄存儲的方式也比較多,比如文本,數(shù)據(jù)庫,云等等,但是最好的選擇還是redis進(jìn)行存儲,這篇文章主要給大家介紹了關(guān)于如何利用redis實現(xiàn)聊天記錄轉(zhuǎn)存功能的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評論