可配置是否开启 redis 缓存,通过 SpringAOP 实现,在 application.yml 中配置

# 是否开启 redis 缓存,true 开启,false 关闭
redisOpen: false
This commit is contained in:
luoxiang 2020-11-04 16:58:29 +08:00
parent 5d748a50ff
commit b4de9aab9d
3 changed files with 45 additions and 0 deletions

View File

@ -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/*

View File

@ -34,6 +34,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- SpringBoot 拦截器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>

View File

@ -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;
}
}