Java Code Examples for redis.clients.jedis.Response#get()

The following examples show how to use redis.clients.jedis.Response#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionCommandsTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void transactionResponseWithError() {
  Transaction t = jedis.multi();
  t.set("foo", "bar");
  Response<Set<String>> error = t.smembers("foo");
  Response<String> r = t.get("foo");
  List<Object> l = t.exec();
  assertEquals(JedisDataException.class, l.get(1).getClass());
  try {
    error.get();
    fail("We expect exception here!");
  } catch (JedisDataException e) {
    // that is fine we should be here
  }
  assertEquals(r.get(), "bar");
}
 
Example 2
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> lrangeltrim(final String key, final int count) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final Response<List<String>> values = pipeline.lrange(key, 0, count);
        pipeline.ltrim(key, count + 1, -1);
        pipeline.sync();
        return values.get();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 3
Source File: JedisClusterPipeline.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
private void innerSync(List<Object> formatted) {
    try {
        Response<?> response;
        Object data;
        for (Client client : clients) {
            response = generateResponse(client.getOne());
            if (null != formatted) {
                data = response.get();
                formatted.add(data);
            }
        }
    } catch (JedisRedirectionException jre) {
        throw jre;
    } finally {
        close();
    }
}
 
Example 4
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void multiWithMassiveRequests() {
  Pipeline p = jedis.pipelined();
  p.multi();

  List<Response<?>> responseList = new ArrayList<Response<?>>();
  for (int i = 0; i < 100000; i++) {
    // any operation should be ok, but shouldn't forget about timeout
    responseList.add(p.setbit("test", 1, true));
  }

  Response<List<Object>> exec = p.exec();
  p.sync();

  // we don't need to check return value
  // if below codes run without throwing Exception, we're ok
  exec.get();

  for (Response<?> resp : responseList) {
    resp.get();
  }
}
 
Example 5
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseable() throws IOException {
  // we need to test with fresh instance of Jedis
  Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
  jedis2.auth("foobared");

  Pipeline pipeline = jedis2.pipelined();
  Response<String> retFuture1 = pipeline.set("a", "1");
  Response<String> retFuture2 = pipeline.set("b", "2");

  pipeline.close();

  // it shouldn't meet any exception
  retFuture1.get();
  retFuture2.get();
}
 
Example 6
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalNestedListsWithBinary() {
  byte[] bScript = SafeEncoder.encode("return { {KEYS[1]} , {2} }");
  byte[] bKey = SafeEncoder.encode("key1");

  Pipeline p = jedis.pipelined();
  Response<Object> result = p.eval(bScript, 1, bKey);
  p.sync();

  List<?> results = (List<?>) result.get();
  assertThat((List<byte[]>) results.get(0), listWithItem(bKey));
  assertThat((List<Long>) results.get(1), listWithItem(2L));
}
 
Example 7
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long sinterstore(final String destination, final String... keys) {
    Assert.hasText(destination);
    if (keys.length == 0) {
        return 0;
    }

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allShards;
        if ((allShards = jedis.getAllShards()).size() == 1) {
            return allShards.iterator().next().sinterstore(destination, keys);
        } else if (allShards.size() > 1) {
            Set<String> interSet = sinter(keys);
            if (!interSet.isEmpty()) {
                final ShardedJedisPipeline pipeline = jedis.pipelined();
                pipeline.del(destination);
                final Response<Long> response = pipeline.sadd(destination, interSet.toArray(new String[interSet.size()]));
                pipeline.sync();
                return response.get();
            }
        }

        return 0;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 8
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long sdiffstore(final String destination, final String... keys) {
    Assert.hasText(destination);
    Assert.notEmpty(keys);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allShards;
        if ((allShards = jedis.getAllShards()).size() == 1) {
            return allShards.iterator().next().sdiffstore(destination, keys);
        } else if (allShards.size() > 1) {
            final Set<String> diffSet = sdiff(keys);
            if (!diffSet.isEmpty()) {
                final ShardedJedisPipeline pipeline = jedis.pipelined();
                pipeline.del(destination);
                final Response<Long> response = pipeline.sadd(destination, diffSet.toArray(new String[diffSet.size()]));
                pipeline.sync();
                return response.get();
            }
        }

        return 0;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 9
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long sreplace(final String key, final String[] oldMembers, final String[] newMembers) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        if (!ArrayUtils.isEmpty(oldMembers)) {
            pipeline.srem(key, oldMembers);
        }

        Response<Long> response = null;
        if (!ArrayUtils.isEmpty(newMembers)) {
            response = pipeline.sadd(key, newMembers);
        }

        pipeline.sync();
        if (response != null) {
            return response.get();
        }

        return 0;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 10
Source File: ShardedJedisPipelineTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisDataException.class)
public void pipelineResponseWithinPipeline() {
  jedis.set("string", "foo");

  ShardedJedisPipeline p = jedis.pipelined();
  Response<String> string = p.get("string");
  string.get();
  p.sync();
}
 
Example 11
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseableWithMulti() throws IOException {
  // we need to test with fresh instance of Jedis
  Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
  jedis2.auth("foobared");

  Pipeline pipeline = jedis2.pipelined();
  Response<String> retFuture1 = pipeline.set("a", "1");
  Response<String> retFuture2 = pipeline.set("b", "2");

  pipeline.multi();

  pipeline.set("a", "a");
  pipeline.set("b", "b");

  pipeline.close();

  try {
    pipeline.exec();
    fail("close should discard transaction");
  } catch (JedisDataException e) {
    assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
  }

  // it shouldn't meet any exception
  retFuture1.get();
  retFuture2.get();
}
 
Example 12
Source File: SomeOperate.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
public static void PipelineTransactions(Jedis jedis, JedisPool jedisPool) {
	try {
		Pipeline pipeLine = jedis.pipelined();
		pipeLine.set("value", "100");
		pipeLine.watch("value");
		pipeLine.multi();// ��������
		pipeLine.incrBy("value", 10);// ����10
		// �Դ������������ʹ���˲�֧�ֵIJ���
		// pipeLine.lpush("value", "error");//ִ�д���IJ���lpush
		pipeLine.incrBy("value", 10);// �ٴε���10
		// ִ��exec����,��ȡ"δ��"�ķ��ؽ��
		Response<List<Object>> listResponse = pipeLine.exec();
		pipeLine.sync();// ����pipeling
		List<Object> result = listResponse.get();
		if (result != null && result.size() > 0) {
			for (Object o : result)
				System.out.println(o.toString());
		}
		// ��Ȼ�����еڶ�������ʧ����,����Ӱ��value��ֵ
		System.out.println("\nvalue is " + jedis.get("value"));
	} catch (Exception e) {
		// jedisPool.returnBrokenResource(jedis);
		e.printStackTrace();
	} finally {
		jedisPool.returnResource(jedis);
	}
}
 
Example 13
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalNestedLists() {
  String script = "return { {KEYS[1]} , {2} }";

  Pipeline p = jedis.pipelined();
  Response<Object> result = p.eval(script, 1, "key1");
  p.sync();

  List<?> results = (List<?>) result.get();
  assertThat((List<String>) results.get(0), listWithItem("key1"));
  assertThat((List<Long>) results.get(1), listWithItem(2L));
}
 
Example 14
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiscardInPipeline() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.multi();
  pipeline.set("foo", "bar");
  Response<String> discard = pipeline.discard();
  Response<String> get = pipeline.get("foo");
  pipeline.sync();
  discard.get();
  get.get();
}
 
Example 15
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long sunionstore(final String destination, final String... keys) {
    Assert.hasText(destination);
    if (keys.length == 0) {
        return 0;
    }

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allShards;
        if ((allShards = jedis.getAllShards()).size() == 1) {
            return allShards.iterator().next().sunionstore(destination, keys);
        } else if (allShards.size() > 1) {
            final Set<String> unionSet = sunion(keys);
            if (!unionSet.isEmpty()) {
                final ShardedJedisPipeline pipeline = jedis.pipelined();
                pipeline.del(destination);
                final Response<Long> response = pipeline.sadd(destination, unionSet.toArray(new String[unionSet.size()]));
                pipeline.sync();
                return response.get();
            }
        }

        return 0;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 16
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisDataException.class)
public void pipelineResponseWithinPipeline() {
  jedis.set("string", "foo");

  Pipeline p = jedis.pipelined();
  Response<String> string = p.get("string");
  string.get();
  p.sync();
}
 
Example 17
Source File: HashesCommandsTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void hgetAllPipeline() {
  Map<byte[], byte[]> bh = new HashMap<byte[], byte[]>();
  bh.put(bbar, bcar);
  bh.put(bcar, bbar);
  jedis.hmset(bfoo, bh);
  Pipeline pipeline = jedis.pipelined();
  Response<Map<byte[], byte[]>> bhashResponse = pipeline.hgetAll(bfoo);
  pipeline.sync();
  Map<byte[], byte[]> bhash = bhashResponse.get();

  assertEquals(2, bhash.size());
  assertArrayEquals(bcar, bhash.get(bbar));
  assertArrayEquals(bbar, bhash.get(bcar));
}
 
Example 18
Source File: TransactionCommandsTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisDataException.class)
public void transactionResponseWithinPipeline() {
  jedis.set("string", "foo");

  Transaction t = jedis.multi();
  Response<String> string = t.get("string");
  string.get();
  t.exec();
}
 
Example 19
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test
public void testPipelinedTransactionResponse() {

  String key1 = "key1";
  String val1 = "val1";

  String key2 = "key2";
  String val2 = "val2";

  String key3 = "key3";
  String field1 = "field1";
  String field2 = "field2";
  String field3 = "field3";
  String field4 = "field4";

  String value1 = "value1";
  String value2 = "value2";
  String value3 = "value3";
  String value4 = "value4";

  Map<String, String> hashMap = new HashMap<String, String>();
  hashMap.put(field1, value1);
  hashMap.put(field2, value2);

  String key4 = "key4";
  Map<String, String> hashMap1 = new HashMap<String, String>();
  hashMap1.put(field3, value3);
  hashMap1.put(field4, value4);

  jedis.set(key1, val1);
  jedis.set(key2, val2);
  jedis.hmset(key3, hashMap);
  jedis.hmset(key4, hashMap1);

  Pipeline pipeline = jedis.pipelined();
  pipeline.multi();

  pipeline.get(key1);
  pipeline.hgetAll(key2);
  pipeline.hgetAll(key3);
  pipeline.get(key4);

  Response<List<Object>> response = pipeline.exec();
  pipeline.sync();

  List<Object> result = response.get();

  assertEquals(4, result.size());

  assertEquals("val1", result.get(0));

  assertTrue(result.get(1) instanceof JedisDataException);

  Map<String, String> hashMapReceived = (Map<String, String>) result.get(2);
  Iterator<String> iterator = hashMapReceived.keySet().iterator();
  String mapKey1 = iterator.next();
  String mapKey2 = iterator.next();
  assertFalse(iterator.hasNext());
  verifyHasBothValues(mapKey1, mapKey2, field1, field2);
  String mapValue1 = hashMapReceived.get(mapKey1);
  String mapValue2 = hashMapReceived.get(mapKey2);
  verifyHasBothValues(mapValue1, mapValue2, value1, value2);

  assertTrue(result.get(3) instanceof JedisDataException);
}
 
Example 20
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test
public void pipelineBinarySafeHashCommands() {
  jedis.hset("key".getBytes(), "f1".getBytes(), "v111".getBytes());
  jedis.hset("key".getBytes(), "f22".getBytes(), "v2222".getBytes());

  Pipeline p = jedis.pipelined();
  Response<Map<byte[], byte[]>> fmap = p.hgetAll("key".getBytes());
  Response<Set<byte[]>> fkeys = p.hkeys("key".getBytes());
  Response<List<byte[]>> fordered = p.hmget("key".getBytes(), "f22".getBytes(), "f1".getBytes());
  Response<List<byte[]>> fvals = p.hvals("key".getBytes());
  p.sync();

  assertNotNull(fmap.get());
  // we have to do these strange contortions because byte[] is not a very
  // good key
  // for a java Map. It only works with equality (you need the exact key
  // object to retrieve
  // the value) I recommend we switch to using ByteBuffer or something
  // similar:
  // http://stackoverflow.com/questions/1058149/using-a-byte-array-as-hashmap-key-java
  Map<byte[], byte[]> map = fmap.get();
  Set<byte[]> mapKeys = map.keySet();
  Iterator<byte[]> iterMap = mapKeys.iterator();
  byte[] firstMapKey = iterMap.next();
  byte[] secondMapKey = iterMap.next();
  assertFalse(iterMap.hasNext());
  verifyHasBothValues(firstMapKey, secondMapKey, "f1".getBytes(), "f22".getBytes());
  byte[] firstMapValue = map.get(firstMapKey);
  byte[] secondMapValue = map.get(secondMapKey);
  verifyHasBothValues(firstMapValue, secondMapValue, "v111".getBytes(), "v2222".getBytes());

  assertNotNull(fkeys.get());
  Iterator<byte[]> iter = fkeys.get().iterator();
  byte[] firstKey = iter.next();
  byte[] secondKey = iter.next();
  assertFalse(iter.hasNext());
  verifyHasBothValues(firstKey, secondKey, "f1".getBytes(), "f22".getBytes());

  assertNotNull(fordered.get());
  assertArrayEquals("v2222".getBytes(), fordered.get().get(0));
  assertArrayEquals("v111".getBytes(), fordered.get().get(1));

  assertNotNull(fvals.get());
  assertEquals(2, fvals.get().size());
  byte[] firstValue = fvals.get().get(0);
  byte[] secondValue = fvals.get().get(1);
  verifyHasBothValues(firstValue, secondValue, "v111".getBytes(), "v2222".getBytes());
}