Skip to content

Commit

Permalink
fix kryo
Browse files Browse the repository at this point in the history
  • Loading branch information
endink committed Jan 1, 2024
1 parent 0f63f26 commit 74f6bdf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RedisCachingAutoConfiguration {

@Bean
@ConditionalOnMissingBean(JsonSmileDataSerializer::class)
fun kryoCacheDataSerializer(customizers: ObjectProvider<IJsonSmileCacheDataSerializerCustomizer>): JsonSmileDataSerializer {
fun jsonSmileDataSerializer(customizers: ObjectProvider<IJsonSmileCacheDataSerializerCustomizer>): JsonSmileDataSerializer {
val smileMapper = JsonSmileDataSerializer.createObjectMapper()
customizers.orderedStream().forEach {
it.customize(smileMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,25 +13,29 @@ import kotlin.reflect.KClass
* @date 2019-09-08
*/
class KryoOptions {
private val registeredClasses:MutableMap<Int, Registration> = mutableMapOf()

data class KryoRegistry<T>(val id: Int, val clazz: Class<T>, val serializer: Serializer<T>?)

private val registeredClasses:MutableMap<Int, KryoRegistry<*>> = mutableMapOf()

var writeBufferSizeBytes = 1024 * 4
var poolSize = -1
var isRegistrationRequired:Boolean = false
var warnUnregisteredClasses:Boolean = false

fun getRegistry(): Array<Registration> {
fun getRegistry(): Array<KryoRegistry<*>> {
return registeredClasses.values.toTypedArray()
}


fun <T : Any> registerClass(id: Int, type: KClass<T>, serializer: Serializer<T>? = 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)
}
}

0 comments on commit 74f6bdf

Please sign in to comment.