最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年08月16日 08:44:26   作者:風(fēng)起風(fēng)落丶  
基于 spring aop 常規(guī)應(yīng)用場景多是用于日志記錄以及實(shí)現(xiàn) redis 分布式鎖,在 github 中也有項(xiàng)目是把它拿來當(dāng)作緩存的異常捕捉,這篇文章主要介紹了基于?Spring?Aop?環(huán)繞通知實(shí)現(xiàn)?Redis?緩存雙刪,需要的朋友可以參考下

基于 spring aop 常規(guī)應(yīng)用場景多是用于日志記錄以及實(shí)現(xiàn) redis 分布式鎖,在 github 中也有項(xiàng)目是把它拿來當(dāng)作緩存的異常捕捉。從而避免影響實(shí)際業(yè)務(wù)的開發(fā);在某天,筆者有個(gè)業(yè)務(wù)開發(fā)是給某個(gè)服務(wù)模塊增加 redis 緩存。增加緩存就會(huì)涉及 redis 刪除。所以筆者就在思考是不是可以用環(huán)繞通知的方式來進(jìn)行實(shí)現(xiàn)

代碼實(shí)現(xiàn)

結(jié)構(gòu)示意圖:

自定義注解 RedisDelByDbUpdate

@Repeatable 表示允許在同一個(gè)地方上使用相同的注解,沒有該注解時(shí)貼相同注解會(huì)報(bào)錯(cuò)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(RedisDelByDbUpdateList.class)
public @interface RedisDelByDbUpdate {
    /**
     * 具體操作得緩存前綴
     */
    String redisPrefix() default "";
 
    /**
     * 具體的緩存名稱,為空則刪除 redisPrefix 下的所有緩存
     */
    String fieldName() default "";
}

自定義注解 RedisDelByUpdateList

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisDelByDbUpdateList {
    
    RedisDelByDbUpdate[] value();
}

具體實(shí)現(xiàn) redis 雙刪邏輯

@Aspect
@Component
@Slf4j
public class RedisDelAspect_bak {
    //環(huán)繞增強(qiáng)
    @Autowired
    private RedisUtil redis;
 
    @Pointcut("@annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdate) " +
            "|| @annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdateList)")
    public void pointCut() {
 
    }
 
    // 線程池定長設(shè)置:最佳線程數(shù)目 = ((線程等待時(shí)間+線程CPU時(shí)間)/線程CPU時(shí)間 )* CPU數(shù)目
    ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(26);
 
    /**
     * 環(huán)繞增強(qiáng)
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
 
        RedisDelByDbUpdate redisDelByDbUpdate = method.getAnnotation(RedisDelByDbUpdate.class);
        if (redisDelByDbUpdate != null) {
            return singleRedisDel(redisDelByDbUpdate, point);
        }
 
        RedisDelByDbUpdateList updateList = method.getAnnotation(RedisDelByDbUpdateList.class);
        return arraysRedisDel(updateList, point);
    }
 
    private Object singleRedisDel(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point) throws Throwable {
        return handle(redisDelByDbUpdate, point, new IObjectCallback<Object>() {
            public <T> T callback() throws Throwable {
                return (T) point.proceed();
            }
        });
    }
 
    private Object arraysRedisDel(RedisDelByDbUpdateList updateList, ProceedingJoinPoint point) throws Throwable {
        RedisDelByDbUpdate[] redisDelByDbUpdates = updateList.value();
        for (int i = 0; i < redisDelByDbUpdates.length; i++) {
            boolean flag = i == redisDelByDbUpdates.length - 1 ? true : false;
 
            Object obj = handle(redisDelByDbUpdates[i], point, new IObjectCallback<Object>() {
                public <T> T callback() throws Throwable {
                    return flag ? (T) point.proceed() : null;
                }
            });
            if (flag) return obj;
        }
        return null;
    }
 
    private Object handle(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point,
                          IObjectCallback<Object> object) throws Throwable {
 
        String prefix = redisDelByDbUpdate.redisPrefix();
        String fieldName = redisDelByDbUpdate.fieldName();
 
        if (ValueUtil.isEmpty(prefix)) {
            log.info("redis緩存前綴不能為空");
            throw new BizException(BaseResponseCode.SYSTEM_BUSY);
        }
 
        Object arg = point.getArgs()[0];
        String key = "";
        String[] redisKeys = null;
 
        if (ValueUtil.isNotEmpty(fieldName)) {
            if (arg instanceof ArrayList) {
                redisKeys = ((ArrayList<?>) arg).stream().map(item -> prefix + item).toArray(String[]::new);
            } else {
                Map<String, Object> map = (Map<String, Object>) JsonUtil.toMap(JsonUtil.toJSON(point.getArgs()[0]));
                key = map.get(fieldName).toString();
            }
        } else {
            // 獲取所有該前綴下緩存
            Set<String> keys = redis.keys(prefix + "*");
            redisKeys = keys.stream().toArray(String[]::new);
        }
 
        // 刪除緩存
        String redisKey = prefix + key;
        delete(redisKey, redisKeys);
        Object result = object.callback();
 
        // 延時(shí)刪除
        try {
            String[] finalRedisKeys = redisKeys;
            threadPool.schedule(new Runnable() {
                @Override
                public void run() {
                    delete(redisKey, finalRedisKeys);
                }
            }, 500, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            log.error("線程池延時(shí)刪除緩存失敗:{}", redisKey);
        }
        return result;
    }
 
    private void delete(String redisKey, String[] redisKeys) {
        if (ValueUtil.isEmpty(redisKeys)) {
            redis.delete(redisKey);
            return;
        }
        redis.del(redisKeys);
    }
}

注解使用示例

public class Test {
    @Override
    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER, fieldName = "ids")
    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER_LIST)
    public void batchDeleted(List<String> ids) throws BizException {
        if (CollectionUtils.isEmpty(ids)) {
            log.info("banner ID 不能為空");
            throw new BizException(BaseResponseCode.PARAM_IS_NOT_NULL);
        }
        try {
            bannerMapper.batchDeleted(ids);
        } catch (Exception e) {
            log.error("==>批量刪除Banner錯(cuò)誤:{}<==", e);
            throw new BizException(BaseResponseCode.SYSTEM_BUSY);
        }
    }
}

到此這篇關(guān)于基于 Spring Aop 環(huán)繞通知實(shí)現(xiàn) Redis 緩存雙刪的文章就介紹到這了,更多相關(guān)Spring Aop Redis 緩存雙刪內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • redis加鎖的幾種方式匯總

    redis加鎖的幾種方式匯總

    這篇文章主要介紹了redis加鎖的幾種方式匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Redis定時(shí)任務(wù)原理的實(shí)現(xiàn)

    Redis定時(shí)任務(wù)原理的實(shí)現(xiàn)

    本文主要是基于?redis?6.2?源碼進(jìn)行分析定時(shí)事件的數(shù)據(jù)結(jié)構(gòu)和常見操作,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Redis MGET命令深度解析

    Redis MGET命令深度解析

    Redis的MGET命令是一種高效的批量讀取操作,可以顯著提高讀取性能,減少網(wǎng)絡(luò)往返的次數(shù),本文從MGET命令的機(jī)制實(shí)現(xiàn)、底層原理、應(yīng)用場景及性能優(yōu)化等多個(gè)維度,深入解析Redis中的MGET命令的工作方式,并對(duì)它與其他批量操作命令的對(duì)比進(jìn)行了詳細(xì)介紹
    2024-09-09
  • Redis設(shè)置鍵的生存時(shí)間或過期時(shí)間的方法詳解

    Redis設(shè)置鍵的生存時(shí)間或過期時(shí)間的方法詳解

    這篇文章主要介紹了Redis如何設(shè)置鍵的生存時(shí)間或過期時(shí)間,通過EXPIRE命令或者PEXIPIRE命令,客戶端可以以秒或者毫秒精度為數(shù)據(jù)庫中的某個(gè)鍵設(shè)置生存時(shí)間,文中有詳細(xì)的代碼供供大家參考,需要的朋友可以參考下
    2024-03-03
  • Redis中切片集群詳解

    Redis中切片集群詳解

    切片集群Redis中,數(shù)據(jù)增多了,是該加內(nèi)存還是加實(shí)例?采用云主機(jī)來運(yùn)行Redis實(shí)例,那么,該如何選擇云主機(jī)的內(nèi)存容量呢?用Redis保存5000萬個(gè)鍵值對(duì),每個(gè)鍵值對(duì)大約是512B方案一:大內(nèi)存云主機(jī):選擇一臺(tái)32GB內(nèi)存的云主機(jī)來部署Redis
    2025-01-01
  • Redis隊(duì)列和阻塞隊(duì)列的實(shí)現(xiàn)

    Redis隊(duì)列和阻塞隊(duì)列的實(shí)現(xiàn)

    本文主要介紹了Redis隊(duì)列和阻塞隊(duì)列的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Redisson之lock()和tryLock()的區(qū)別及說明

    Redisson之lock()和tryLock()的區(qū)別及說明

    這篇文章主要介紹了Redisson之lock()和tryLock()的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Redis解決緩存雪崩、穿透和擊穿的問題(Redis使用必看)

    Redis解決緩存雪崩、穿透和擊穿的問題(Redis使用必看)

    這篇文章主要給大家介紹了Redis解決緩存雪崩、緩存穿透、緩存擊穿的解決方案,文中有詳細(xì)的圖文介紹,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Redis連接失?。嚎蛻舳薎P不在白名單中的問題分析與解決方案

    Redis連接失?。嚎蛻舳薎P不在白名單中的問題分析與解決方案

    在現(xiàn)代分布式系統(tǒng)中,Redis作為一種高性能的內(nèi)存數(shù)據(jù)庫,被廣泛應(yīng)用于緩存、消息隊(duì)列、會(huì)話存儲(chǔ)等場景,然而,在實(shí)際使用過程中,我們可能會(huì)遇到各種連接問題,其中“客戶端IP不在白名單中”是一個(gè)常見的錯(cuò)誤,本文將從錯(cuò)誤分析、原因排查、解決方案等詳細(xì)探討如何解決這一問題
    2025-01-01
  • Redis基本數(shù)據(jù)類型List常用操作命令

    Redis基本數(shù)據(jù)類型List常用操作命令

    這篇文章主要為大家介紹了Redis數(shù)據(jù)類型List常用命令操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論

通城县| 永寿县| 敦化市| 西乌珠穆沁旗| 汕头市| 丰原市| 来凤县| 荔波县| 松溪县| 华阴市| 灵丘县| 潍坊市| 射洪县| 大厂| 肥东县| 南江县| 天水市| 塔河县| 九龙县| 中江县| 织金县| 儋州市| 公安县| 上饶市| 栾川县| 龙南县| 海盐县| 池州市| 太保市| 八宿县| 襄城县| 通州区| 湄潭县| 顺昌县| 大名县| 巧家县| 灌云县| 彰化市| 孟州市| 墨玉县| 广东省|