package com.gstanzer.supervise.cache;
import com.ctg.itrdc.cache.pool.CtgJedisPool;
import com.ctg.itrdc.cache.pool.CtgJedisPoolConfig;
import com.ctg.itrdc.cache.vjedis.jedis.JedisPoolConfig;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
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.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import java.util.ArrayList;
import java.util.List;
/**
* 缓存配置类
*
* @author: tangbingbing
* @date: 2024/7/22 14:28
*/
@Configuration
public class CacheConfig {
@Value("${access.cq.redis.host1}")
private String reidsHost1;
@Value("${access.cq.redis.host2}")
private String reidsHost2;
@Value("${access.cq.redis.port}")
private int port;
@Value("${access.cq.redis.password}")
private String password;
@Value("${access.cq.redis.group}")
private String group;
@Value("${access.cq.redis.max-total}")
private int maxTotal;
@Value("${access.cq.redis.max-idle}")
private int maxIdle;
@Value("${access.cq.redis.min-idle}")
private int minIdle;
@Value("${access.cq.redis.max-wait}")
private int maxWait;
@Value("${access.cq.redis.period}")
private int period;
@Value("${access.cq.redis.monitor-timeout}")
private int monitorTimeout;
@Bean
@Conditional(RedisCondition.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 创建并返回RedisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
// 设置value的序列化器
//使用Jackson 2,将对象序列化为JSON
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//json转对象类,不设置默认的会将json转成hashmap
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// json中会显示类型
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
@Conditional(RedisCondition.class)
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
// 创建并返回RedisMessageListenerContainer
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
// 监听所有库的key过期事件
container.setConnectionFactory(connectionFactory);
return container;
}
@Bean
@Conditional(CtgCacheCondition.class)
public CtgJedisPool ctgJedisPool() {
// 创建并返回CtgJedisPool
List<HostAndPort> hostAndPortList = new ArrayList();
HostAndPort host1 = new HostAndPort(reidsHost1, port);
HostAndPort host2 = new HostAndPort(reidsHost2, port);
hostAndPortList.add(host1);
hostAndPortList.add(host2);
GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxTotal); // 最大连接数(空闲+使用中)
poolConfig.setMaxIdle(maxIdle); //最大空闲连接数
poolConfig.setMinIdle(minIdle); //保持的最小空闲连接数
poolConfig.setMaxWaitMillis(maxWait); //借出连接时最大的等待时间
CtgJedisPoolConfig config = new CtgJedisPoolConfig(hostAndPortList);
config.setDatabase(group)
.setPassword(password)
.setPoolConfig(poolConfig)
.setPeriod(period)
.setMonitorTimeout(monitorTimeout);
CtgJedisPool pool = new CtgJedisPool(config);
return pool;
}
}
|