I am using Redis with Spring boot. I am using String as Key and the value against it as a List of complex POJO. Below is my config:
@Configuration
@EnableCaching
@Slf4j
public class RedisCacheConfig extends CachingConfigurerSupport {
private static final long DEFAULT_CACHE_EXPIRES = 60;
@Bean
public RedisTemplate<String, Object> redisTemplate(final RedisConnectionFactory redisConnectionFactory) {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
setRedisTemplateConfigValues(redisConnectionFactory, template);
return template;
}
@Bean
public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory) {
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
return RedisCacheManager
.builder(redisConnectionFactory)
.cacheDefaults(createCacheConfiguration())
.withInitialCacheConfigurations(cacheConfigurations).build();
}
private static RedisCacheConfiguration createCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(DEFAULT_CACHE_EXPIRES));
}
private <T> void setRedisTemplateConfigValues(final RedisConnectionFactory redisConnectionFactory,
final RedisTemplate<String, T> template) {
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
}
}
The cahcing is working fine and serialization/deserialization via my app also seems to work. But when i use redis-cli, I see the below when i use the command KEYS *
1) "schools::ABC"
Now the value against ABC key should be a list of complex objects. But when I do GET "schools::ABC"
I get the value with strange characters as below:
\xac\xed\x00\x05sr\x00\x13java.util.ArrayListx\x81\xd2\x1d\x99\xc7a\x9d\x03\x00\x01I\x00\x04sizexp\x00\x00\x00\x01w\x04\x00\x00\x00\x01sr\x00(com.example. and so on....
Why is it so?
Also, I tried updating GenericJackson2JsonRedisSerializer in the config for valueSerializer to Jackson2JsonRedisSerializer. The result was no different.
Also, I tried to get the TYPE of the key, I get the result as 'String', so the list is getting stored as String.