3

I am using redis as my caching store in my application. For that i am using the following redis configuration:

@Configuration
@EnableCaching
public class SpringRedisConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
    JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
    connectionFactory.setHostName("localhost");
    connectionFactory.setPort(6379);
    return connectionFactory;
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());


    RedisSerializer<String> redisSerializer = new StringRedisSerializer();

    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);

    //template.setConnectionFactory(factory);
    redisTemplate.setKeySerializer(redisSerializer);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);


    return redisTemplate;
}
}

and in my services to store data in cache, i am using @Cacheable as follows:

@Cacheable(value = "institute_detail_cache", key = "'inst_'+#instituteId")
public Map<String, String> getDetailsFromCache(Long instituteId) {
    log.info("not found in cache.....");

    Map<String, String> map = new HashMap<>();
    map.put("name", "kayv");
    map.put("age", "20");

    return map;
}

The code is working fine and storing the data in cache as object. Here is the value as i got from redis cli:

enter image description here

But my requirement is to store the data(value) as json, with key-value pairs. That is why i have used Jackson2JsonRedisSerializer for the value serialisation.

Can anyone suggest what wrong i am doing?

And what should i need to implement to make my classes to be store as json instead of map as i used in this example?

KayV
  • 12,987
  • 11
  • 98
  • 148
  • Off Topic: in order to store JSON to Redis, you can try [redis-protobuf](https://github.com/sewenew/redis-protobuf) or [redisjson](https://github.com/RedisJSON/RedisJSON). – for_stack Nov 05 '19 at 05:37
  • Could you answer this https://stackoverflow.com/questions/70350448/hgetall-in-redis-cli-showing-the-results-like-x00-xff-what-is-really-happening – JITHIN_PATHROSE Dec 15 '21 at 09:44

1 Answers1

1

Using GenericJackson2JsonRedisSerializer class instead of Jackson2JsonRedisSerializer class solved my issue for me.

Created a new Configuration class (RedisCacheManagerConfiguration.java) instead of old SpringRedisConfig.java.

@Configuration
@EnableCaching
public class RedisCacheManagerConfiguration {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public CacheManager redisCacheManager() {
        RedisSerializationContext.SerializationPair<Object> jsonSerializer =
                RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());

        return RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(redisConnectionFactory)
                .cacheDefaults(
                        RedisCacheConfiguration.defaultCacheConfig()
                                .entryTtl(Duration.ofDays(1))
                                .serializeValuesWith(jsonSerializer)
                )
                .build();
    }

}
KayV
  • 12,987
  • 11
  • 98
  • 148