|
|
@ -3,16 +3,46 @@ package com.ccsens.util.config; |
|
|
|
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.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.data.redis.cache.RedisCacheConfiguration; |
|
|
|
import org.springframework.data.redis.cache.RedisCacheManager; |
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|
|
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
|
|
|
import org.springframework.data.redis.serializer.RedisSerializationContext; |
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer; |
|
|
|
import redis.clients.jedis.JedisPoolConfig; |
|
|
|
|
|
|
|
import java.time.Duration; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author 23778 |
|
|
|
*/ |
|
|
|
@Configuration |
|
|
|
public class RedisConfig { |
|
|
|
|
|
|
|
@Value("${mybatisCache.host:}") |
|
|
|
private String hostName; |
|
|
|
@Value("${mybatisCache.password:}") |
|
|
|
private String password; |
|
|
|
@Value("${mybatisCache.port:}") |
|
|
|
private int port; |
|
|
|
@Value("${mybatisCache.database:}") |
|
|
|
private int database; |
|
|
|
@Value("${mybatisCache.timeout:}") |
|
|
|
private int timeout; |
|
|
|
@Value("${mybatisCache.jedis.pool.max-active:}") |
|
|
|
private int maxActive; |
|
|
|
@Value("${mybatisCache.jedis.pool.max-idle:}") |
|
|
|
private int maxIdle; |
|
|
|
@Value("${mybatisCache.jedis.pool.min-idle:}") |
|
|
|
private int minIdle; |
|
|
|
@Value("${mybatisCache.jedis.pool.max-wait:}") |
|
|
|
private long maxWait; |
|
|
|
|
|
|
|
@Bean |
|
|
|
@SuppressWarnings("all") |
|
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { |
|
|
@ -35,4 +65,70 @@ public class RedisConfig { |
|
|
|
template.afterPropertiesSet(); |
|
|
|
return template; |
|
|
|
} |
|
|
|
|
|
|
|
@Bean("mybatisCache") |
|
|
|
@SuppressWarnings("all") |
|
|
|
public RedisTemplate<Object, Object> mybatisCacheRedisTemplate() { |
|
|
|
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); |
|
|
|
RedisConnectionFactory factory = this.getRedisConnectionFactory(); |
|
|
|
template.setConnectionFactory(factory); |
|
|
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
|
|
|
ObjectMapper om = new ObjectMapper(); |
|
|
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
|
|
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
|
|
|
jackson2JsonRedisSerializer.setObjectMapper(om); |
|
|
|
// value序列化方式采用jackson
|
|
|
|
template.setValueSerializer(jackson2JsonRedisSerializer); |
|
|
|
// hash的value序列化方式采用jackson
|
|
|
|
template.setHashValueSerializer(jackson2JsonRedisSerializer); |
|
|
|
template.afterPropertiesSet(); |
|
|
|
return template; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 负责建立Factory的连接工厂类 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private RedisConnectionFactory getRedisConnectionFactory() { |
|
|
|
|
|
|
|
JedisConnectionFactory jedisFactory = new JedisConnectionFactory(); |
|
|
|
jedisFactory.setHostName(hostName); |
|
|
|
jedisFactory.setPort(port); |
|
|
|
jedisFactory.setPassword(password); |
|
|
|
jedisFactory.setDatabase(database); |
|
|
|
jedisFactory.setTimeout(timeout); |
|
|
|
// 进行连接池配置
|
|
|
|
JedisPoolConfig poolConfig = new JedisPoolConfig(); |
|
|
|
poolConfig.setMaxTotal(maxActive); |
|
|
|
poolConfig.setMaxIdle(maxIdle); |
|
|
|
poolConfig.setMinIdle(minIdle); |
|
|
|
poolConfig.setMaxWaitMillis(maxWait); |
|
|
|
|
|
|
|
jedisFactory.setPoolConfig(poolConfig); |
|
|
|
// 初始化连接池配置
|
|
|
|
jedisFactory.afterPropertiesSet(); |
|
|
|
return jedisFactory; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
public RedisCacheManager redisCacheManager() { |
|
|
|
RedisConnectionFactory factory = getRedisConnectionFactory(); |
|
|
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
|
|
|
ObjectMapper om = new ObjectMapper(); |
|
|
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
|
|
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
|
|
|
jackson2JsonRedisSerializer.setObjectMapper(om); |
|
|
|
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() |
|
|
|
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) |
|
|
|
.entryTtl(Duration.ofSeconds(300)); |
|
|
|
|
|
|
|
RedisCacheManager redisCacheManager = RedisCacheManager.builder(factory) |
|
|
|
.cacheDefaults(config) |
|
|
|
.transactionAware() |
|
|
|
.build(); |
|
|
|
return redisCacheManager; |
|
|
|
} |
|
|
|
} |