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

springboot+redis緩存的實(shí)現(xiàn)方案

 更新時(shí)間:2025年03月05日 09:33:24   作者:李晨豪 Lch  
本文介紹了Spring Boot與Redis結(jié)合實(shí)現(xiàn)緩存的三種方案:注解方式、注解切面類(lèi)方式和使用樣例,通過(guò)這些方案,可以有效地提高應(yīng)用程序的性能和響應(yīng)速度

springboot+redis緩存方案

一、注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
 * @author lch
 * @date 2023/8/03 09:29
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisCache {

    /**
     * key,支持 spring el表達(dá)式
     */
    String key();

    /**
     * 統(tǒng)一格式:服務(wù)名:場(chǎng)景,如:info
     */
    String prefix();

    /**
     * 過(guò)期時(shí)間
     */
    int expireTime() default 36000;

    /**
     * 時(shí)間單位
     */
    TimeUnit timeunit() default TimeUnit.SECONDS;

}

二、注解切面類(lèi)

import com.dewav.tms.framework.aspectj.lang.annotation.RedisCache;
import com.dewav.tms.framework.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * @author lch
 * @date 2023/8/03 09:29
 */
@Component
@Aspect
@Slf4j
public class RedisCacheAspect {

    private final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();

    private final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();

    @Resource
    private RedisService redisService;

    @Around("@annotation(redisCache)")
    public Object queryByRedisCache(ProceedingJoinPoint pjp, RedisCache redisCache) {
        int expireTime = redisCache.expireTime();
        String key = redisCache.key();
        String prefix = redisCache.prefix();
        Assert.hasText(key, "@RedisCache key不能為空!");
        Assert.hasText(prefix, "@RedisCache prefix不能為空!");
        String evaluateExpression = evaluateExpression(key, pjp);

        String redisKey = prefix + evaluateExpression;
        Object obj = null;
        try {
            if (null != redisKey){
                if (redisService.hasKey(redisKey)){
                    obj = redisService.getCacheObject(redisKey);
                    return obj;
                }
            }
        } catch (Exception e) {
            log.error("從Redis獲取"+redisKey+"失?。?+e.getMessage());
        }

        try{
            obj = pjp.proceed();
        }catch(Throwable e){
            log.error("執(zhí)行異常", e);
            throw new RuntimeException(e.getMessage());
        }
        if (null != redisKey){
            redisService.setCacheObject(redisKey, obj, expireTime, TimeUnit.SECONDS);
        }

        return obj;
    }

    /**
     * 解析el表達(dá)式
     *
     * @param expression
     * @param point
     * @return
     */
    private String evaluateExpression(String expression, ProceedingJoinPoint point) {
        // 獲取目標(biāo)對(duì)象
        Object target = point.getTarget();
        // 獲取方法參數(shù)
        Object[] args = point.getArgs();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Method method = methodSignature.getMethod();

        EvaluationContext context = new MethodBasedEvaluationContext(target, method, args, parameterNameDiscoverer);
        Expression exp = spelExpressionParser.parseExpression(expression);
        return exp.getValue(context, String.class);
    }
}

三、使用樣例

    @Override
    @RedisCache(key = "{#customerId + ':' + #dictType}", prefix = CacheConstants.SYS_DICT_KEY)
    public List<SysDictData> selectDictDataByType(Long customerId, String dictType) {
        return dictDataMapper.selectDictDataByType(customerId, dictType);
    }

	@Override
    @RedisCache(key = "#apiKey", prefix = CacheConstants.OPEN_KEY + "apiInfo:apiKey:")
    public EdiApiInfo getByApiKey(String apiKey) {
        return baseMapper.selectOne(new QueryWrapper<EdiApiInfo>()
                .eq("api_key", apiKey)
                .eq("status", true)
                .eq("del_flag", false));
    }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java設(shè)計(jì)模式之動(dòng)態(tài)代理

    Java設(shè)計(jì)模式之動(dòng)態(tài)代理

    今天小編就為大家分享一篇關(guān)于Java設(shè)計(jì)模式之動(dòng)態(tài)代理,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • JAVA 格式化日期、時(shí)間的方法

    JAVA 格式化日期、時(shí)間的方法

    這篇文章主要介紹了JAVA 格式化日期、時(shí)間的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 基于Java回顧之多線程同步的使用詳解

    基于Java回顧之多線程同步的使用詳解

    在這篇文章里,我們關(guān)注線程同步的話題。這是比多線程更復(fù)雜,稍不留意,我們就會(huì)“掉到坑里”,而且和單線程程序不同,多線程的錯(cuò)誤是否每次都出現(xiàn),也是不固定的,這給調(diào)試也帶來(lái)了很大的挑戰(zhàn)
    2013-05-05
  • SpringBoot web靜態(tài)資源配置詳解

    SpringBoot web靜態(tài)資源配置詳解

    這篇文章主要介紹了SpringBoot web靜態(tài)資源配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Spring Session的使用示例

    Spring Session的使用示例

    最近團(tuán)隊(duì)一個(gè)項(xiàng)目需要使用Session,之前沒(méi)有在實(shí)際項(xiàng)目中使用過(guò)Spring-Session,這里記錄一下使用的過(guò)程
    2021-06-06
  • 五種SpringBoot實(shí)現(xiàn)數(shù)據(jù)加密存儲(chǔ)的方式總結(jié)

    五種SpringBoot實(shí)現(xiàn)數(shù)據(jù)加密存儲(chǔ)的方式總結(jié)

    這篇文章主要為大家詳細(xì)介紹了五種常見(jiàn)數(shù)據(jù)加密存儲(chǔ)的方法(結(jié)合SpringBoot和MyBatisPlus框架進(jìn)行實(shí)現(xiàn)),文中的示例代碼講解詳細(xì),需要的可以參考下
    2023-11-11
  • Pulsar負(fù)載均衡原理及優(yōu)化方案詳解

    Pulsar負(fù)載均衡原理及優(yōu)化方案詳解

    這篇文章主要為大家介紹了Pulsar負(fù)載均衡原理及優(yōu)化方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • k8s+springboot+CronJob定時(shí)任務(wù)部署實(shí)現(xiàn)

    k8s+springboot+CronJob定時(shí)任務(wù)部署實(shí)現(xiàn)

    本文主要介紹了k8s+springboot+CronJob定時(shí)任務(wù)部署實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java單例模式的8種寫(xiě)法(推薦)

    Java單例模式的8種寫(xiě)法(推薦)

    這篇文章主要介紹了Java單例模式的8種寫(xiě)法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評(píng)論