|
@@ -0,0 +1,65 @@
|
|
|
+package com.zhentao.common.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.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.connection.RedisPassword;
|
|
|
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
|
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class RedisConfig {
|
|
|
+
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
+ private String host;
|
|
|
+
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
+ private int port;
|
|
|
+
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
+ private String password;
|
|
|
+ @Value("${spring.redis.database}")
|
|
|
+ private int database;
|
|
|
+ @Bean
|
|
|
+ public RedisConnectionFactory redisConnectionFactory() {
|
|
|
+ RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
|
|
|
+ config.setHostName(host);
|
|
|
+ config.setPort(port);
|
|
|
+ config.setPassword(RedisPassword.of(password));
|
|
|
+ config.setDatabase(database);
|
|
|
+
|
|
|
+ return new LettuceConnectionFactory(config);
|
|
|
+ }
|
|
|
+ @Bean
|
|
|
+ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
|
|
+ RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
|
+ template.setConnectionFactory(factory);
|
|
|
+
|
|
|
+ // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
|
|
|
+ Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
+ mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
+ serializer.setObjectMapper(mapper);
|
|
|
+
|
|
|
+ template.setValueSerializer(serializer);
|
|
|
+ template.setKeySerializer(new StringRedisSerializer());
|
|
|
+ template.afterPropertiesSet();
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
|
|
|
+ StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
|
|
|
+ stringRedisTemplate.setConnectionFactory(factory);
|
|
|
+ return stringRedisTemplate;
|
|
|
+ }
|
|
|
+}
|