List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo("localhost", 6379);
si.setPassword("foobared");
shards.add(si);
si = new JedisShardInfo("localhost", 6380);
si.setPassword("foobared");
shards.add(si);
Then, there are two ways of using ShardedJedis
. Direct connections or by using ShardedJedisPool
. For reliable operation, the latter has to be used in a multithreaded environment.
2.a) Direct connection:
ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("a", "foo");
jedis.disconnect;
2.b) Pooled connection:
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.set("a", "foo");
.... // do your work here
pool.returnResource(jedis);
.... // a few moments later
ShardedJedis jedis2 = pool.getResource();
jedis.set("z", "bar");
pool.returnResource(jedis);
pool.destroy();
Above example shows how to use ShardedJedis
.
In my current setup, I am using RedisTemplate
and JedisConnectionFactory
.
My question is
How do I use
ShardedJedis
withRedisTemplate
?
I think it doesn't directly support your case. RedisTemplate offers a high-level abstraction for Redis interactions. While RedisConnection offers low level methods that accept and return binary values (byte arrays).
See: Working with Objects through RedisTemplate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With