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)代理
今天小編就為大家分享一篇關(guān)于Java設(shè)計(jì)模式之動(dòng)態(tài)代理,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01五種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-11k8s+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-07Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04