diff --git a/redis-starter/src/main/kotlin/com/labijie/caching/redis/configuration/RedisCachingAutoConfiguration.kt b/redis-starter/src/main/kotlin/com/labijie/caching/redis/configuration/RedisCachingAutoConfiguration.kt index 864b4d3..d5c57d5 100644 --- a/redis-starter/src/main/kotlin/com/labijie/caching/redis/configuration/RedisCachingAutoConfiguration.kt +++ b/redis-starter/src/main/kotlin/com/labijie/caching/redis/configuration/RedisCachingAutoConfiguration.kt @@ -62,7 +62,7 @@ class RedisCachingAutoConfiguration { @Bean @ConditionalOnMissingBean(JsonSmileDataSerializer::class) - fun kryoCacheDataSerializer(customizers: ObjectProvider): JsonSmileDataSerializer { + fun jsonSmileDataSerializer(customizers: ObjectProvider): JsonSmileDataSerializer { val smileMapper = JsonSmileDataSerializer.createObjectMapper() customizers.orderedStream().forEach { it.customize(smileMapper) diff --git a/redis/src/main/kotlin/com/labijie/caching/redis/CacheDataSerializerRegistry.kt b/redis/src/main/kotlin/com/labijie/caching/redis/CacheDataSerializerRegistry.kt index 7b36ebc..289e52d 100644 --- a/redis/src/main/kotlin/com/labijie/caching/redis/CacheDataSerializerRegistry.kt +++ b/redis/src/main/kotlin/com/labijie/caching/redis/CacheDataSerializerRegistry.kt @@ -19,28 +19,8 @@ object CacheDataSerializerRegistry { fun getSerializer(name: String): ICacheDataSerializer { val serializerName = name.lowercase() return serializers.getOrPut(serializerName) { - createSerializer(serializerName) - } - } - - private fun createSerializer(serializerName: String): ICacheDataSerializer { - return when (serializerName) { - JacksonCacheDataSerializer.NAME -> { - val ser = JacksonCacheDataSerializer() - serializers[serializerName] = ser - ser - } - KryoCacheDataSerializer.NAME -> { - val ser = KryoCacheDataSerializer(KryoOptions()) - serializers[serializerName] = ser - ser - } - JsonSmileDataSerializer.NAME -> { - val ser = JsonSmileDataSerializer() - serializers[serializerName] = ser - ser - } - else -> throw CacheException("Cant find cache data serializer with name '$serializerName'") + logger.warn("Unable to find caching data serializer named: '${serializerName}', jackson serializer was used.") + return JacksonCacheDataSerializer() } } diff --git a/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoCacheDataSerializer.kt b/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoCacheDataSerializer.kt index 7178644..9cf2a58 100644 --- a/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoCacheDataSerializer.kt +++ b/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoCacheDataSerializer.kt @@ -80,7 +80,11 @@ class KryoCacheDataSerializer(val kryoOptions: KryoOptions) : ICacheDataSerializ if (it.id <= 100) { throw CacheException("Kryo register class id must be greater than 100 ( start with 101 )") } - this.register(it) + if(it.serializer != null) { + this.register(it.clazz, it.serializer, it.id) + }else { + this.register(it.clazz, it.id) + } } } } diff --git a/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoOptions.kt b/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoOptions.kt index 4209ff4..8b24a8a 100644 --- a/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoOptions.kt +++ b/redis/src/main/kotlin/com/labijie/caching/redis/serialization/KryoOptions.kt @@ -1,5 +1,6 @@ package com.labijie.caching.redis.serialization +import com.esotericsoftware.kryo.Kryo import com.esotericsoftware.kryo.Registration import com.esotericsoftware.kryo.Serializer import com.labijie.caching.CacheException @@ -12,25 +13,29 @@ import kotlin.reflect.KClass * @date 2019-09-08 */ class KryoOptions { - private val registeredClasses:MutableMap = mutableMapOf() + + data class KryoRegistry(val id: Int, val clazz: Class, val serializer: Serializer?) + + private val registeredClasses:MutableMap> = mutableMapOf() var writeBufferSizeBytes = 1024 * 4 var poolSize = -1 var isRegistrationRequired:Boolean = false var warnUnregisteredClasses:Boolean = false - fun getRegistry(): Array { + fun getRegistry(): Array> { return registeredClasses.values.toTypedArray() } + fun registerClass(id: Int, type: KClass, serializer: Serializer? = null) { if (id <= 100) { - throw IllegalArgumentException("Kryo register class id must be greater than 100 ( start with 101 )") + throw IllegalArgumentException("Kryo register class id must be greater than 100 ( start with 101 ), want ot register: ${id}: ${type.simpleName}") } val existed = registeredClasses.getOrDefault(id, null) - if (existed != null) { - throw CacheException("Class registered to kryo with id '$id' was existed. ( existed: ${existed.type.simpleName}, registering: ${type.simpleName} )") + existed?.let { + throw CacheException("Class registered to kryo with id '$id' was existed. ( existed: ${existed.clazz.simpleName}, registering: ${type.simpleName} )") } - registeredClasses[id] = Registration(type.java, serializer, id) + registeredClasses[id] = KryoRegistry(id, type.java, serializer) } } \ No newline at end of file