以下是直接从Jedis GitHub页面复制的Jedis文档:
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);
然后,使用ShardedJedis
有两种方式。直接连接或使用ShardedJedisPool
。为了可靠的操作,后者必须在多线程环境中使用。
2.a) 直接连接:
ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("a", "foo");
jedis.disconnect;
2.b) 汇集连接:
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();
上面的示例展示了如何使用ShardedJedis
。
在我的当前设置中,我正在使用RedisTemplate
和JedisConnectionFactory
。
我的问题是:
我该如何在
RedisTemplate
中使用ShardedJedis
?