mirror of https://github.com/elunez/eladmin
代码优化
parent
43c09d150d
commit
e2b0b93cca
|
@ -6,6 +6,7 @@ import com.alibaba.fastjson.parser.ParserConfig;
|
||||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.zhengjie.utils.StringUtils;
|
import me.zhengjie.utils.StringUtils;
|
||||||
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||||
|
@ -26,6 +27,8 @@ import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
|
@ -83,13 +86,22 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||||
@Override
|
@Override
|
||||||
public KeyGenerator keyGenerator() {
|
public KeyGenerator keyGenerator() {
|
||||||
return (target, method, params) -> {
|
return (target, method, params) -> {
|
||||||
StringBuilder sb = new StringBuilder();
|
Map<String,Object> container = new HashMap<>();
|
||||||
sb.append(target.getClass().getName());
|
Class<?> targetClassClass = target.getClass();
|
||||||
sb.append(method.getName());
|
// 类地址
|
||||||
for (Object obj : params) {
|
container.put("class",targetClassClass.toGenericString());
|
||||||
sb.append(JSON.toJSONString(obj).hashCode());
|
// 方法名称
|
||||||
|
container.put("methodName",method.getName());
|
||||||
|
// 包名称
|
||||||
|
container.put("package",targetClassClass.getPackage());
|
||||||
|
// 参数列表
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
container.put(String.valueOf(i),params[i]);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
// 转为JSON字符串
|
||||||
|
String jsonString = JSON.toJSONString(container);
|
||||||
|
// 做SHA256 Hash计算,得到一个SHA256摘要作为Key
|
||||||
|
return DigestUtils.sha256Hex(jsonString);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,9 @@ public class RedisServiceImpl implements RedisService {
|
||||||
@Value("${loginCode.expiration}")
|
@Value("${loginCode.expiration}")
|
||||||
private Long expiration;
|
private Long expiration;
|
||||||
|
|
||||||
|
@Value("${jwt.online}")
|
||||||
|
private String onlineKey;
|
||||||
|
|
||||||
public RedisServiceImpl(RedisTemplate redisTemplate) {
|
public RedisServiceImpl(RedisTemplate redisTemplate) {
|
||||||
this.redisTemplate = redisTemplate;
|
this.redisTemplate = redisTemplate;
|
||||||
}
|
}
|
||||||
|
@ -40,7 +43,7 @@ public class RedisServiceImpl implements RedisService {
|
||||||
Set<String> keys = redisTemplate.keys(key);
|
Set<String> keys = redisTemplate.keys(key);
|
||||||
for (String s : keys) {
|
for (String s : keys) {
|
||||||
// 过滤掉权限的缓存
|
// 过滤掉权限的缓存
|
||||||
if (s.contains("role::loadPermissionByUser") || s.contains("user::loadUserByUsername") || s.contains("online:token")) {
|
if (s.contains("role::loadPermissionByUser") || s.contains("user::loadUserByUsername") || s.contains(onlineKey)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
RedisVo redisVo = new RedisVo(s, Objects.requireNonNull(redisTemplate.opsForValue().get(s)).toString());
|
RedisVo redisVo = new RedisVo(s, Objects.requireNonNull(redisTemplate.opsForValue().get(s)).toString());
|
||||||
|
@ -60,7 +63,7 @@ public class RedisServiceImpl implements RedisService {
|
||||||
@Override
|
@Override
|
||||||
public void deleteAll() {
|
public void deleteAll() {
|
||||||
Set<String> keys = redisTemplate.keys( "*");
|
Set<String> keys = redisTemplate.keys( "*");
|
||||||
redisTemplate.delete(keys.stream().filter(s -> !s.contains("online:token")).collect(Collectors.toList()));
|
redisTemplate.delete(keys.stream().filter(s -> !s.contains(onlineKey)).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue