Skip to content
Bluetape4k docs1.11

SerializationContext and templates

Latest stable Based on Bluetape4k release 1.11.0

Redis templates have separate serializers for keys, values, hash keys, and hash values. Use the same format for logical values stored both directly and in hashes.

val context = redisSerializationContext<String, Any> {
key(RedisSerializer.string())
value(RedisBinarySerializers.LZ4Fory)
hashKey(RedisSerializer.string())
hashValue(RedisBinarySerializers.LZ4Fory)
}

The DSL delegates to the Spring Data Redis builder and calls build(). Explicitly configure each slot your application uses.

The first redisSerializationContextOf overload applies the key and value serializers to the corresponding hash slots.

val context = redisSerializationContextOf<String, Any>(
keySerializer = RedisSerializer.string(),
valueSerializer = RedisBinarySerializers.ZstdFory,
)

The final builder block can override individual slots. Different value and hash-value formats are possible, but they make operational inspection harder.

The value-only overload fixes keys and hash keys to StringRedisSerializer.UTF_8.

val context = redisSerializationContextOf<Any>(
valueSerializer = RedisBinarySerializers.LZ4Kryo,
)

This fits most application keyspaces. Object serialization for keys is risky because class changes can change key identity.

@Bean
fun ordersRedisTemplate(
factory: ReactiveRedisConnectionFactory,
): ReactiveRedisTemplate<String, Any> {
val context = redisSerializationContextOf<Any>(
valueSerializer = RedisBinarySerializers.LZ4Fory,
)
return ReactiveRedisTemplate(factory, context)
}

The module does not choose bean names, qualifiers, or @Primary. When several templates exist, align each bean name with its key prefix and schema.

@Bean
fun ordersRedisTemplate(factory: RedisConnectionFactory) =
RedisTemplate<String, Any>().apply {
connectionFactory = factory
keySerializer = RedisSerializer.string()
valueSerializer = RedisBinarySerializers.LZ4Fory
hashKeySerializer = RedisSerializer.string()
hashValueSerializer = RedisBinarySerializers.LZ4Fory
afterPropertiesSet()
}

When synchronous and reactive templates share keys, test cross-reading between them rather than testing only self round trips.

redisSerializationContext(defaultSerializer) starts the builder with that default. Explicit key, value, and hash calls override their slots. Public keyspaces are easier to maintain when every used slot is visible in configuration.