Replace Redisson RedisClient with Spring Boot Redis

83 views Asked by At

I have an existing Redisson RedisClient and corresponding sentinel configuration. I am fetching some information by passing a key and using a method which calls the default getset method in Redisson.

public <T> Set<T> getSet(String key) throws NotFoundException {
        try {
            return redissonClient.getSet(key, new JsonJacksonCodec());
        } catch (RedisException redisException) {
            logger.error("Error retrieving data from cache "+ redisException+ " for ProductKey "+ key);
            throw new NotFoundException(ErrorCode.NO_DATA_FOUND);
        }
}

This is fetching me the correct information I need. Now I want to change this approach and instead of Redisson I want to use Spring Data Redis. I added the necessary dependency and I am able to create the RedisTemplate and am able to populate LettuceConnectionFactory with the same database, master, sentinel address and timeouts as the existing one.

@Bean
 protected LettuceConnectionFactory redisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
    .master(masterName);
  //redisProperties.getSentinel().getNodes().forEach(s -> sentinelConfig.sentinel(s, redisProperties.getPort()));
  sentinelConfig.sentinel(host,port);
  sentinelConfig.setPassword(getPassword);
  sentinelConfig.setDatabase(getDatabaseIndex);

  LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
    .commandTimeout(redisCommandTimeout).readFrom(ReadFrom.REPLICA_PREFERRED).build();
  return new LettuceConnectionFactory(sentinelConfig, clientConfig);
 }

 @Bean
 public RedisTemplate<String, Object> redisTemplate() {
  final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  redisTemplate.setKeySerializer(new StringRedisSerializer());
  redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
  redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
  redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
  redisTemplate.setConnectionFactory(redisConnectionFactory());
  return redisTemplate;
 }

Now, I want to replace the existing getSet method with a method which will fetch me the same information as the getSet method. I could not find a similar get or fetch method in RedisTemplate which will return me a Set or RSet data for a key and a JsonJacksonCodec. How do I proceed to replace the existing functionality of passing a key and codec to fetch RSet data if I am using RedisTemplate instead of Redisson RedisCleint?

0

There are 0 answers