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 {
|
||||
|
||||
|
||||
/**
|
||||
* redis缓存key前缀
|
||||
*/
|
||||
private String keyPrefix;
|
||||
|
||||
/**
|
||||
* 线程池数量,默认值 = 当前处理核数量 * 2
|
||||
*/
|
||||
|
@ -0,0 +1,50 @@
|
||||
package com.ruoyi.framework.handler;
|
||||
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.redisson.api.NameMapper;
|
||||
|
||||
/*
|
||||
* redis缓存key前缀处理
|
||||
* @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…
x
Reference in New Issue
Block a user