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

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

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

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表達式
     */
    String key();

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

    /**
     * 過期時間
     */
    int expireTime() default 36000;

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

}

二、注解切面類

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表達式
     *
     * @param expression
     * @param point
     * @return
     */
    private String evaluateExpression(String expression, ProceedingJoinPoint point) {
        // 獲取目標對象
        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));
    }

總結

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

相關文章

  • Java設計模式之動態(tài)代理

    Java設計模式之動態(tài)代理

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

    JAVA 格式化日期、時間的方法

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

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

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

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

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

    Spring Session的使用示例

    最近團隊一個項目需要使用Session,之前沒有在實際項目中使用過Spring-Session,這里記錄一下使用的過程
    2021-06-06
  • 五種SpringBoot實現(xiàn)數(shù)據(jù)加密存儲的方式總結

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

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

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

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

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

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

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

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

    Java實現(xiàn)學生成績管理系統(tǒng)

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

最新評論