Merge branch 'master' of 192.168.8.205:yunzhic/ruoyi-cloud
This commit is contained in:
commit
1c68ffca15
|
|
@ -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,46 @@
|
|||
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;
|
||||
|
||||
/**
|
||||
* 自定义RedisTemplate,支持选库
|
||||
*
|
||||
* @author gxx
|
||||
*/
|
||||
public class GridntRedisTemplate<K, V> 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,34 +1,33 @@
|
|||
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.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.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 {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
|
||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
||||
public GridntRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory)
|
||||
{
|
||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||
// 关闭共享连接
|
||||
connectionFactory.setShareNativeConnection(false);
|
||||
GridntRedisTemplate<Object, Object> template = new GridntRedisTemplate(connectionFactory);
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
|
||||
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
|
|
|||
Loading…
Reference in New Issue