From b4de9aab9db53e65baa231017494dbd5ed9361a8 Mon Sep 17 00:00:00 2001 From: luoxiang Date: Wed, 4 Nov 2020 16:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BC=80=E5=90=AF=20redis=20=E7=BC=93=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20SpringAOP=20=E5=AE=9E=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8=20application.yml=20=E4=B8=AD=E9=85=8D=E7=BD=AE=20#?= =?UTF-8?q?=20=E6=98=AF=E5=90=A6=E5=BC=80=E5=90=AF=20redis=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98=EF=BC=8Ctrue=20=E5=BC=80=E5=90=AF=EF=BC=8Cfalse=20?= =?UTF-8?q?=E5=85=B3=E9=97=AD=20redisOpen:=20false?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 4 +++ ruoyi-common/pom.xml | 5 +++ .../ruoyi/common/core/redis/RedisAspect.java | 36 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisAspect.java diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index e43091e47..e7301b51c 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -14,6 +14,8 @@ ruoyi: addressEnabled: false # 验证码类型 math 数组计算 char 字符验证 captchaType: math + # 是否开启 redis 缓存,true 开启,false 关闭 + redisOpen: false # 开发环境配置 server: @@ -124,3 +126,5 @@ xss: excludes: /system/notice/* # 匹配链接 urlPatterns: /system/*,/monitor/*,/tool/* + + diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 46823fe71..aa8887dad 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -34,6 +34,11 @@ org.springframework.boot spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-aop + diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisAspect.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisAspect.java new file mode 100644 index 000000000..c261ded19 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisAspect.java @@ -0,0 +1,36 @@ +package com.ruoyi.common.core.redis; + +import com.ruoyi.common.exception.CustomException; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class RedisAspect { + private Logger logger = LoggerFactory.getLogger(getClass()); + /** + * 是否开启redis缓存 true开启 false关闭 + */ + @Value("${ruoyi.redisOpen: false}") + private boolean open; + + @Around("execution(* com.ruoyi.common.core.redis.RedisCache.*(..))") + public Object around(ProceedingJoinPoint point) throws Throwable { + Object result = null; + if(open){ + try{ + result = point.proceed(); + }catch (Exception e){ + logger.error("redis error", e); + throw new CustomException("Redis服务异常"); + } + } + return result; + } +} +