|
|
@@ -0,0 +1,121 @@
|
|
|
+package cn.com.ctop.job.bytedance.config;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
|
+import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
+import com.fasterxml.jackson.core.JsonParser;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
+import org.springframework.cache.annotation.EnableCaching;
|
|
|
+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.cache.RedisCacheWriter;
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Configuration
|
|
|
+@EnableCaching
|
|
|
+public class RedisCacheConfig {
|
|
|
+
|
|
|
+ private static final long DEFAULT_DURATION = 5 * 60L;
|
|
|
+
|
|
|
+ private final static String DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss.SSSXXX";
|
|
|
+
|
|
|
+ public static final List<CacheSpec> CACHE_SPEC_LIST = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CacheManager cacheManager(RedisConnectionFactory factory) {
|
|
|
+ //对象的序列化
|
|
|
+ ObjectMapper objectMapper = getNewInstance();
|
|
|
+ objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
|
|
+ Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
|
+ jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
|
|
+
|
|
|
+ // 全局redis缓存过期时间,默认60秒
|
|
|
+ RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
+ .computePrefixWith(name -> name + ":")
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer.UTF_8))
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
|
|
|
+ .entryTtl(Duration.ofSeconds(DEFAULT_DURATION))
|
|
|
+ .disableCachingNullValues();
|
|
|
+
|
|
|
+ Map<String, RedisCacheConfiguration> redisCacheConfigMap = new HashMap<>();
|
|
|
+ // 缓存N秒
|
|
|
+ CACHE_SPEC_LIST.add(new CacheSpec("getBytedanceAdvertiserByAd", 12 * 3600L));
|
|
|
+ CACHE_SPEC_LIST.add(new CacheSpec("getDZAppIdByChannelId", 12 * 3600L));
|
|
|
+ CACHE_SPEC_LIST.forEach(cache -> {
|
|
|
+ long duration = DEFAULT_DURATION;
|
|
|
+ if (cache.getExpireTtl() != null && cache.getExpireTtl() > 0) {
|
|
|
+ duration = cache.getExpireTtl();
|
|
|
+ }
|
|
|
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
+ // 默认name + 两个:,改为一个:
|
|
|
+ .computePrefixWith(name -> name + ":")
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer.UTF_8))
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
|
|
|
+ .entryTtl(Duration.ofSeconds(duration));
|
|
|
+ redisCacheConfigMap.put(cache.getCacheName(), config);
|
|
|
+ });
|
|
|
+
|
|
|
+ return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(factory))
|
|
|
+ .cacheDefaults(defaultConfig)
|
|
|
+ .withInitialCacheConfigurations(redisCacheConfigMap)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ @AllArgsConstructor
|
|
|
+ public class CacheSpec {
|
|
|
+ private String cacheName;
|
|
|
+ private Long expireTtl;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * default ttl 60s
|
|
|
+ *
|
|
|
+ * @param cacheName
|
|
|
+ */
|
|
|
+ public CacheSpec(String cacheName) {
|
|
|
+ this(cacheName, DEFAULT_DURATION);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ObjectMapper getNewInstance() {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+ objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
|
|
|
+ objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
|
|
|
+ objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
|
|
+ objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // 忽略映射属性
|
|
|
+ //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ objectMapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMATTER));
|
|
|
+ // 序列化 LocalDateTime
|
|
|
+ JavaTimeModule timeModule = new JavaTimeModule();
|
|
|
+ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss", Locale.CHINA);
|
|
|
+ timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
|
|
|
+ timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
|
|
|
+ objectMapper.registerModule(timeModule);
|
|
|
+
|
|
|
+ return objectMapper;
|
|
|
+ }
|
|
|
+}
|