1

Originally in our code we were using RedisTemplate in conjuction with JedisConnectionFactory and JedisPoolConfig as we were using a Redis on a single node:

   @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> getRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        // ...
        return template;
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory(Configuration config) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(config.get(HOST));
        jedisConnectionFactory.setPort(config.getInt(PORT));
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(createJedisPoolConfig(config));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    JedisPoolConfig createJedisPoolConfig(Config config) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // ...
        return poolConfig;
    }

We are now using JedisCluster as we are using a Redis cluster.

When we used RedisTemplate we used a number of commands such as redisTemplate.hasKey(cacheKey) to check if the key exists and redisTemplate.opsForValue().set among many others relevant to RedisTemplate.

These methods don't appear to be available for JedisCluster.

Is there a way I can use RedisTemplate in conjunction with JedisCluster to avoid re-coding these methods and just utilise the methods RedisTemplate offers?

Or are there alternative commands that can be used in place of the ones offered by RedisTemplate?

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
  • 1
    Thank you so much @ErmiyaEskandary. I got it working. You are an absolute legend! Both approaches worked fine but I ended up using the one in the stackoverflow comment you mentioned to minimalise the code change required. – codemonkey1010 Feb 15 '22 at 09:29
  • You're more than welcome! Make sure to upvote the other answer if it helped and welcome to Stack Overflow. – Ermiya Eskandary Feb 15 '22 at 09:49

1 Answers1

1

s there a way I can use RedisTemplate in conjunction with JedisCluster to avoid re-coding these methods and just utilise the methods RedisTemplate offers?

Yes, refer to this SO answer.

Or are there alternative commands that can be used in place of the ones offered by RedisTemplate?

For future reference, also yes.

RedisTemplate.hasKey(cacheKey) is an interface to the Redis EXISTS command.
JedisCluster.exists(cacheKey) is the equivalent.

RedisTemplate.opsForValue().set() is an interface to operations on strings.
JedisCluster.set("key", "value") is the equivalent.

Use the RedisTemplate docs to figure out the Redis command being used and then map it to the equivalent in Jedis using their documentation.

A search in your favourite search engine for "Redis [COMMAND] in Jedis" should get you what you need.

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44