You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.5 KiB
102 lines
3.5 KiB
|
|
|
|
package com.xujie.sys.config;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.data.redis.cache.CacheKeyPrefix;
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
import org.springframework.data.redis.core.*;
|
|
import org.springframework.data.redis.listener.ChannelTopic;
|
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
/**
|
|
* Redis配置
|
|
*
|
|
*
|
|
*/
|
|
@Configuration
|
|
@Slf4j
|
|
public class RedisConfig {
|
|
|
|
@Bean
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
|
|
redisTemplate.setValueSerializer(new StringRedisSerializer());
|
|
// value序列化方式采用jackson
|
|
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
|
return redisTemplate;
|
|
}
|
|
|
|
|
|
/**
|
|
* spring redis 默认生成key方式,包含::号
|
|
*
|
|
* @param prefix the prefix
|
|
* @param key the key
|
|
* @return string
|
|
*/
|
|
public String simpleKeyGenerator(String prefix, String key) {
|
|
return CacheKeyPrefix.simple().compute(prefix) + key;
|
|
}
|
|
|
|
/**
|
|
* 创建Redis消息监听器
|
|
*
|
|
* @param connectionFactory
|
|
* @return
|
|
*/
|
|
@Bean
|
|
@Primary
|
|
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
|
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
container.setConnectionFactory(connectionFactory);
|
|
return container;
|
|
}
|
|
|
|
/**
|
|
* 监听指定的库
|
|
* 这个地方,我当初设置的是库是 1,我链接的库是 4
|
|
* 但是最后Listener还是能收到其他库key过期的消息,不知道为啥。。。
|
|
* 建议大家改成 * ,直接监听所有库哈。不过这样消息会变多哈。
|
|
*/
|
|
@Bean
|
|
public ChannelTopic expiredTopic() {
|
|
return new ChannelTopic("__keyevent@*__:expired");
|
|
}
|
|
|
|
@Bean
|
|
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
return redisTemplate.opsForHash();
|
|
}
|
|
|
|
@Bean
|
|
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
|
|
return redisTemplate.opsForValue();
|
|
}
|
|
|
|
@Bean
|
|
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
return redisTemplate.opsForList();
|
|
}
|
|
|
|
@Bean
|
|
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
return redisTemplate.opsForSet();
|
|
}
|
|
|
|
@Bean
|
|
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
return redisTemplate.opsForZSet();
|
|
}
|
|
}
|