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?
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.