skip to Main Content

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:

xacxedx00x05srx00x13java.util.ArrayListxx81xd2x1dx99xc7ax9dx03x00x01Ix00x04sizexpx00x00x00x01wx04x00x00x00x01srx00(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.

3

Answers


  1. The reason why you are seeing those strange characters is because it is binary data.

    You configured the GenericJackson2JsonRedisSerializer as the serializer for the values. This class internally calls ObjectMapper#writeValueAsBytes(…) which as the name suggest converts the value you put in to bytes.

    This is to be expected. Redis does not directly support storing Java objects. It however does support storing binary data. By using Jackson we can convert our Java object to bytes and store that in Redis. Of course the other way around also works.

    Login or Signup to reply.
  2. Your Redis configuration seems fine but you didn’t mention the type of your storing procedure. Maybe you have to use another way of storing your data. For example, you can try

    redisTemplate.opsForHash().put('yourKey','yourHashKey', objectValue);
    
    Login or Signup to reply.
  3. You could try setting the serializer in the cachemanager bean as below

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisSerializationContext.SerializationPair<Object> jsonSerializer = RedisSerializationContext.SerializationPair
                .fromSerializer(new GenericJackson2JsonRedisSerializer());
    
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(jsonSerializer);
    
        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
                .cacheDefaults(config).build();
    }
    

    This worked for me .I can also see the value in the redis gui as a json

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search