|
@@ -1,5 +1,11 @@
|
|
|
package com.gxzc.zen.common.config.cache.redis
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonTypeInfo
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule
|
|
|
+import com.fasterxml.jackson.databind.ser.std.StdSerializer
|
|
|
import org.apache.commons.pool2.impl.GenericObjectPool
|
|
|
import org.springframework.beans.factory.ObjectProvider
|
|
|
import org.springframework.beans.factory.annotation.Qualifier
|
|
@@ -10,6 +16,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisProperties
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|
|
import org.springframework.cache.CacheManager
|
|
|
import org.springframework.cache.annotation.EnableCaching
|
|
|
+import org.springframework.cache.support.NullValue
|
|
|
import org.springframework.context.annotation.Bean
|
|
|
import org.springframework.context.annotation.Configuration
|
|
|
import org.springframework.context.annotation.Primary
|
|
@@ -26,8 +33,10 @@ import org.springframework.data.redis.serializer.StringRedisSerializer
|
|
|
import org.springframework.util.Assert
|
|
|
import org.springframework.util.StringUtils
|
|
|
import redis.clients.jedis.JedisPoolConfig
|
|
|
+import java.io.IOException
|
|
|
import java.net.URI
|
|
|
import java.net.URISyntaxException
|
|
|
+import java.text.SimpleDateFormat
|
|
|
import java.util.*
|
|
|
|
|
|
/**
|
|
@@ -50,10 +59,53 @@ class RedisConfig {
|
|
|
val stringRedisSerializer = StringRedisSerializer()
|
|
|
keySerializer = stringRedisSerializer
|
|
|
hashKeySerializer = stringRedisSerializer
|
|
|
- valueSerializer = GenericJackson2JsonRedisSerializer()
|
|
|
+ // mapper
|
|
|
+ val classPropertyTypeName: String? = null
|
|
|
+ valueSerializer = GenericJackson2JsonRedisSerializer(ObjectMapper().apply {
|
|
|
+ dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
|
|
+ registerModule(SimpleModule().addSerializer(NullValueSerializer(classPropertyTypeName)))
|
|
|
+ if (StringUtils.hasText(classPropertyTypeName)) {
|
|
|
+ enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, classPropertyTypeName)
|
|
|
+ } else {
|
|
|
+ enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
|
|
|
+ }
|
|
|
+ })
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * [StdSerializer] adding class information required by default typing. This allows de-/serialization of
|
|
|
+ * [NullValue].
|
|
|
+ *
|
|
|
+ * @author Christoph Strobl
|
|
|
+ * @since 1.8
|
|
|
+ */
|
|
|
+ private class NullValueSerializer : StdSerializer<NullValue> {
|
|
|
+ companion object {
|
|
|
+ private const val serialVersionUID = 1999052150548658809L
|
|
|
+ }
|
|
|
+
|
|
|
+ private val classIdentifier: String
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param classIdentifier can be null and will be defaulted to `@class`.
|
|
|
+ */
|
|
|
+ constructor(classIdentifier: String?) : super(NullValue::class.java) {
|
|
|
+ this.classIdentifier = if (StringUtils.hasText(classIdentifier)) classIdentifier!! else "@class"
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * (non-Javadoc)
|
|
|
+ * @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
|
|
|
+ */
|
|
|
+ @Throws(IOException::class)
|
|
|
+ override fun serialize(value: NullValue, jgen: JsonGenerator, provider: SerializerProvider) {
|
|
|
+ jgen.writeStartObject()
|
|
|
+ jgen.writeStringField(classIdentifier, NullValue::class.java.name)
|
|
|
+ jgen.writeEndObject()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Redis connection configuration.
|
|
|
*/
|