Search in sources :

Example 36 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisPipelineTest method setUp.

@Before
public void setUp() throws Exception {
    Jedis jedis = new Jedis(redis1.getHost(), redis1.getPort());
    jedis.auth("foobared");
    jedis.flushAll();
    jedis.disconnect();
    jedis = new Jedis(redis2.getHost(), redis2.getPort());
    jedis.auth("foobared");
    jedis.flushAll();
    jedis.disconnect();
    JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(), redis1.getPort());
    JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(), redis2.getPort());
    shardInfo1.setPassword("foobared");
    shardInfo2.setPassword("foobared");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(shardInfo1);
    shards.add(shardInfo2);
    this.jedis = new ShardedJedis(shards);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Before(org.junit.Before)

Example 37 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisPipelineTest method testSyncWithNoCommandQueued.

@Test
public void testSyncWithNoCommandQueued() {
    JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(), redis1.getPort());
    JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(), redis2.getPort());
    shardInfo1.setPassword("foobared");
    shardInfo2.setPassword("foobared");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(shardInfo1);
    shards.add(shardInfo2);
    ShardedJedis jedis2 = new ShardedJedis(shards);
    ShardedJedisPipeline pipeline = jedis2.pipelined();
    pipeline.sync();
    jedis2.close();
    jedis2 = new ShardedJedis(shards);
    pipeline = jedis2.pipelined();
    List<Object> resp = pipeline.syncAndReturnAll();
    assertTrue(resp.isEmpty());
    jedis2.close();
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) ShardedJedisPipeline(redis.clients.jedis.ShardedJedisPipeline) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 38 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisPoolTest method checkConnectionWithDefaultPort.

@Test
public void checkConnectionWithDefaultPort() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis jedis = pool.getResource();
    jedis.set("foo", "bar");
    assertEquals("bar", jedis.get("foo"));
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Example 39 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisTest method checkKeyTags.

@Test
public void checkKeyTags() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
    shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
    ShardedJedis jedis = new ShardedJedis(shards, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
    assertEquals(jedis.getKeyTag("foo"), "foo");
    assertEquals(jedis.getKeyTag("foo{bar}"), "bar");
    // default pattern is
    assertEquals(jedis.getKeyTag("foo{bar}}"), "bar");
    // non greedy
    // Key tag may appear
    assertEquals(jedis.getKeyTag("{bar}foo"), "bar");
    // anywhere
    // Key tag may appear
    assertEquals(jedis.getKeyTag("f{bar}oo"), "bar");
    // anywhere
    JedisShardInfo s1 = jedis.getShardInfo("abc{bar}");
    JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
    assertSame(s1, s2);
    List<String> keys = getKeysDifferentShard(jedis);
    JedisShardInfo s3 = jedis.getShardInfo(keys.get(0));
    JedisShardInfo s4 = jedis.getShardInfo(keys.get(1));
    assertNotSame(s3, s4);
    ShardedJedis jedis2 = new ShardedJedis(shards);
    assertEquals(jedis2.getKeyTag("foo"), "foo");
    assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");
    JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0) + "{bar}");
    JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1) + "{bar}");
    assertNotSame(s5, s6);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 40 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class HashingBenchmark method main.

public static void main(String[] args) throws UnknownHostException, IOException {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    JedisShardInfo shard = new JedisShardInfo(hnp1.getHost(), hnp1.getPort());
    shard.setPassword("foobared");
    shards.add(shard);
    shard = new JedisShardInfo(hnp2.getHost(), hnp2.getPort());
    shard.setPassword("foobared");
    shards.add(shard);
    ShardedJedis jedis = new ShardedJedis(shards);
    Collection<Jedis> allShards = jedis.getAllShards();
    for (Jedis j : allShards) {
        j.flushAll();
    }
    long begin = Calendar.getInstance().getTimeInMillis();
    for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
        String key = "foo" + n;
        jedis.set(key, "bar" + n);
        jedis.get(key);
    }
    long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
    jedis.disconnect();
    System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
Also used : Jedis(redis.clients.jedis.Jedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo)

Aggregations

ShardedJedis (redis.clients.jedis.ShardedJedis)45 Test (org.junit.Test)36 JedisShardInfo (redis.clients.jedis.JedisShardInfo)24 ArrayList (java.util.ArrayList)22 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)22 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)22 Jedis (redis.clients.jedis.Jedis)14 URI (java.net.URI)4 ShardedJedisPipeline (redis.clients.jedis.ShardedJedisPipeline)4 Serializable (java.io.Serializable)2 Before (org.junit.Before)2