|
@@ -1,8 +1,10 @@
|
|
package com.qmth.themis.business.config;
|
|
package com.qmth.themis.business.config;
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
|
import com.qmth.themis.business.constant.SystemConstant;
|
|
import com.qmth.themis.business.constant.SystemConstant;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -32,8 +34,7 @@ public class RedisConfig {
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
|
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
|
- return new RedisCacheManager(
|
|
|
|
- RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
|
|
|
|
|
|
+ return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
|
|
this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_DEFAULT_EXPIRE_TIME), // 默认策略,未配置的 key 会使用这个
|
|
this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_DEFAULT_EXPIRE_TIME), // 默认策略,未配置的 key 会使用这个
|
|
this.getRedisCacheConfigurationMap() // 指定 key 策略
|
|
this.getRedisCacheConfigurationMap() // 指定 key 策略
|
|
);
|
|
);
|
|
@@ -41,25 +42,31 @@ public class RedisConfig {
|
|
|
|
|
|
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
|
|
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
|
|
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
|
|
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
|
|
- redisCacheConfigurationMap.put(SystemConstant.userOauth, this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
|
|
|
|
- redisCacheConfigurationMap.put(SystemConstant.studentOauth, this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
|
|
|
|
- redisCacheConfigurationMap.put(SystemConstant.configCache, this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_EXPIRE_TIME));
|
|
|
|
|
|
+ redisCacheConfigurationMap.put(SystemConstant.userOauth,
|
|
|
|
+ this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
|
|
|
|
+ redisCacheConfigurationMap.put(SystemConstant.studentOauth,
|
|
|
|
+ this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_OAUTH_EXPIRE_TIME));
|
|
|
|
+ redisCacheConfigurationMap.put(SystemConstant.configCache,
|
|
|
|
+ this.getRedisCacheConfigurationWithTtl(SystemConstant.REDIS_EXPIRE_TIME));
|
|
return redisCacheConfigurationMap;
|
|
return redisCacheConfigurationMap;
|
|
}
|
|
}
|
|
|
|
|
|
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Long seconds) {
|
|
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Long seconds) {
|
|
- Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
|
|
|
|
+ Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
|
|
|
|
+ Object.class);
|
|
ObjectMapper om = new ObjectMapper();
|
|
ObjectMapper om = new ObjectMapper();
|
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
- om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
|
|
|
+ om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
|
|
|
|
+ JsonTypeInfo.As.WRAPPER_ARRAY);
|
|
|
|
+ //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
|
|
|
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
|
|
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
|
|
- redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
|
|
|
|
- RedisSerializationContext
|
|
|
|
- .SerializationPair
|
|
|
|
- .fromSerializer(jackson2JsonRedisSerializer)
|
|
|
|
- ).entryTtl(Duration.ofSeconds(seconds));
|
|
|
|
|
|
+ redisCacheConfiguration = redisCacheConfiguration.serializeKeysWith(
|
|
|
|
+ RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
|
|
|
+ .serializeValuesWith(
|
|
|
|
+ RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
|
|
|
|
+ .entryTtl(Duration.ofSeconds(seconds));
|
|
return redisCacheConfiguration;
|
|
return redisCacheConfiguration;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -72,11 +79,15 @@ public class RedisConfig {
|
|
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
- mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
|
|
|
+ mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
|
|
|
|
+ JsonTypeInfo.As.WRAPPER_ARRAY);
|
|
|
|
+ //mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
serializer.setObjectMapper(mapper);
|
|
serializer.setObjectMapper(mapper);
|
|
template.setValueSerializer(serializer);
|
|
template.setValueSerializer(serializer);
|
|
|
|
+ template.setHashValueSerializer(serializer);
|
|
//使用StringRedisSerializer来序列化和反序列化redis的key值
|
|
//使用StringRedisSerializer来序列化和反序列化redis的key值
|
|
template.setKeySerializer(new StringRedisSerializer());
|
|
template.setKeySerializer(new StringRedisSerializer());
|
|
|
|
+ template.setHashKeySerializer(new StringRedisSerializer());
|
|
template.afterPropertiesSet();
|
|
template.afterPropertiesSet();
|
|
return template;
|
|
return template;
|
|
}
|
|
}
|