添加redis选库
This commit is contained in:
parent
9c2b84d761
commit
db84d796c6
|
|
@ -4,6 +4,7 @@ dependencies {
|
|||
implementation "com.alibaba:fastjson:1.2.75"
|
||||
implementation "com.fasterxml.jackson.core:jackson-databind:2.12.0"
|
||||
implementation "org.springframework.boot:spring-boot-starter-data-redis:2.3.4.RELEASE"
|
||||
implementation group: 'redis.clients', name: 'jedis', version: '3.5.1'
|
||||
}
|
||||
|
||||
description = "ruoyi-common-redis"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
package com.ruoyi.common.redis.configure;
|
||||
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnection;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 自定义RedisTemplate,支持选库
|
||||
*
|
||||
* @author gxx
|
||||
*/
|
||||
@Component
|
||||
public class GridntRedisTemplate extends StringRedisTemplate {
|
||||
|
||||
public GridntRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
super.setConnectionFactory(connectionFactory);
|
||||
}
|
||||
|
||||
public static ThreadLocal<Integer> REDIS_DB_INDEX = new ThreadLocal<>() {
|
||||
@Override
|
||||
protected Integer initialValue() {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
|
||||
try {
|
||||
Integer dbIndex = REDIS_DB_INDEX.get();
|
||||
if (dbIndex != null) {
|
||||
if (connection instanceof JedisConnection) {
|
||||
if (((JedisConnection) connection).getNativeConnection().getDB() != dbIndex) {
|
||||
connection.select(dbIndex);
|
||||
}
|
||||
} else {
|
||||
connection.select(dbIndex);
|
||||
}
|
||||
} else {
|
||||
connection.select(0);
|
||||
}
|
||||
} finally {
|
||||
//REDIS_DB_INDEX.remove();
|
||||
}
|
||||
return super.preProcessConnection(connection, existingConnection);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +1,47 @@
|
|||
package com.ruoyi.common.redis.configure;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* redis配置
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class RedisConfig extends CachingConfigurerSupport
|
||||
{
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.redis.port}")
|
||||
private int port;
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
|
||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
||||
public LettuceConnectionFactory lettuceConnectionFactory() {
|
||||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
||||
redisStandaloneConfiguration.setHostName(host);
|
||||
redisStandaloneConfiguration.setPort(port);
|
||||
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration);
|
||||
// 关闭共享连接
|
||||
factory.setShareNativeConnection(false);
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GridntRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory)
|
||||
{
|
||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||
GridntRedisTemplate template = new GridntRedisTemplate(connectionFactory);
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
|
||||
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.ruoyi.common.redis.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
|
@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit;
|
|||
public class RedisService
|
||||
{
|
||||
@Autowired
|
||||
@Qualifier("gridntRedisTemplate")
|
||||
public RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue