!201 redission处理增加前缀

* redission处理判断无前缀则不处理
* redission处理增加前缀
feature/model
抓蛙师 2 years ago committed by 疯狂的狮子Li
parent ae707d340b
commit ce7536df9f

@ -138,6 +138,8 @@ spring:
ssl: false
redisson:
# redis key前缀
keyPrefix:
# 线程池数量
threads: 4
# Netty线程池数量

@ -141,6 +141,8 @@ spring:
ssl: false
redisson:
# redis key前缀
keyPrefix:
# 线程池数量
threads: 16
# Netty线程池数量

@ -3,6 +3,7 @@ package com.ruoyi.framework.config;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.framework.config.properties.RedissonProperties;
import com.ruoyi.framework.handler.KeyPrefixHandler;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
@ -48,6 +49,7 @@ public class RedisConfig extends CachingConfigurerSupport {
if (ObjectUtil.isNotNull(singleServerConfig)) {
// 使用单机模式
config.useSingleServer()
.setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))//设置redis key前缀
.setTimeout(singleServerConfig.getTimeout())
.setClientName(singleServerConfig.getClientName())
.setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout())
@ -59,6 +61,7 @@ public class RedisConfig extends CachingConfigurerSupport {
RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();
if (ObjectUtil.isNotNull(clusterServersConfig)) {
config.useClusterServers()
.setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))//设置redis key前缀
.setTimeout(clusterServersConfig.getTimeout())
.setClientName(clusterServersConfig.getClientName())
.setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout())

@ -18,7 +18,12 @@ import java.util.List;
@Component
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {
/**
* rediskey
*/
private String keyPrefix;
/**
* 线, = * 2
*/

@ -0,0 +1,50 @@
package com.ruoyi.framework.handler;
import com.ruoyi.common.utils.StringUtils;
import org.redisson.api.NameMapper;
/*
* rediskey
* @author ye
* @create 2022/7/14 17:44
*/
public class KeyPrefixHandler implements NameMapper {
private final String keyPrefix;
//前缀为空 则返回空前缀
public KeyPrefixHandler(String keyPrefix) {
this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":";
}
//增加前缀
@Override
public String map(String name) {
if (StringUtils.isBlank(name)) {
return null;
}
if (StringUtils.isBlank(keyPrefix)) {
return name;
}
if (!name.startsWith(keyPrefix)) {
return keyPrefix + name;
} else {
return name;
}
}
//去除前缀
@Override
public String unmap(String name) {
if (StringUtils.isBlank(name)) {
return null;
}
if (StringUtils.isBlank(keyPrefix)) {
return name;
}
if (name.startsWith(keyPrefix)) {
return name.substring(keyPrefix.length());
}
return name;
}
}
Loading…
Cancel
Save