Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ShardedJedis with RedisTemplate

Following is Jedis documentation directly copied from jedis github page:

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 with RedisTemplate?

like image 500
hrishikeshp19 Avatar asked Apr 13 '15 23:04

hrishikeshp19


1 Answers

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

like image 85
Nebras Avatar answered Oct 11 '22 23:10

Nebras