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

The following examples show how to use redis.clients.jedis.Pipeline#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: JedisPipelineDemo.java    From Redis-4.x-Cookbook with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    //Connecting to localhost Redis server
    Jedis jedis = new Jedis("localhost");

    //Create a Pipeline
    Pipeline pipeline = jedis.pipelined();
    //Add commands to pipeline
    pipeline.set("mykey", "myvalue");
    pipeline.sadd("myset", "value1", "value2");
    Response<String> stringValue = pipeline.get("mykey");
    Response<Long> noElementsInSet = pipeline.scard("myset");
    //Send commands
    pipeline.sync();
    //Handle responses
    System.out.printf("mykey: %s\n", stringValue.get());
    System.out.printf("Number of Elements in set: %d\n", noElementsInSet.get());
    System.exit(0);
}
 
Example 2
Source File: PipelinedGetSetBenchmark.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws UnknownHostException, IOException {
  Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
  jedis.connect();
  jedis.auth("foobared");
  jedis.flushAll();

  long begin = Calendar.getInstance().getTimeInMillis();

  Pipeline p = jedis.pipelined();
  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    String key = "foo" + n;
    p.set(key, "bar" + n);
    p.get(key);
  }
  p.sync();

  long elapsed = Calendar.getInstance().getTimeInMillis() - begin;

  jedis.disconnect();

  System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
 
Example 3
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void multiWithSync() {
  jedis.set("foo", "314");
  jedis.set("bar", "foo");
  jedis.set("hello", "world");
  Pipeline p = jedis.pipelined();
  Response<String> r1 = p.get("bar");
  p.multi();
  Response<String> r2 = p.get("foo");
  p.exec();
  Response<String> r3 = p.get("hello");
  p.sync();

  // before multi
  assertEquals("foo", r1.get());
  // It should be readable whether exec's response was built or not
  assertEquals("314", r2.get());
  // after multi
  assertEquals("world", r3.get());
}
 
Example 4
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvalKeyAndArg() {
  String key = "test";
  String arg = "3";
  String script = "redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])";

  Pipeline p = jedis.pipelined();
  p.set(key, "0");
  Response<Object> result0 = p.eval(script, Arrays.asList(key), Arrays.asList(arg));
  p.incr(key);
  Response<Object> result1 = p.eval(script, Arrays.asList(key), Arrays.asList(arg));
  Response<String> result2 = p.get(key);
  p.sync();

  assertNull(result0.get());
  assertNull(result1.get());
  assertEquals("13", result2.get());
}
 
Example 5
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvalKeyAndArgWithBinary() {
  // binary
  byte[] bKey = SafeEncoder.encode("test");
  byte[] bArg = SafeEncoder.encode("3");
  byte[] bScript = SafeEncoder
      .encode("redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])");

  Pipeline bP = jedis.pipelined();
  bP.set(bKey, SafeEncoder.encode("0"));
  Response<Object> bResult0 = bP.eval(bScript, Arrays.asList(bKey), Arrays.asList(bArg));
  bP.incr(bKey);
  Response<Object> bResult1 = bP.eval(bScript, Arrays.asList(bKey), Arrays.asList(bArg));
  Response<byte[]> bResult2 = bP.get(bKey);
  bP.sync();

  assertNull(bResult0.get());
  assertNull(bResult1.get());
  assertArrayEquals(SafeEncoder.encode("13"), bResult2.get());
}
 
Example 6
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvalshaKeyAndArg() {
  String key = "test";
  String arg = "3";
  String script = "redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])";
  String sha1 = jedis.scriptLoad(script);

  assertTrue(jedis.scriptExists(sha1));

  Pipeline p = jedis.pipelined();
  p.set(key, "0");
  Response<Object> result0 = p.evalsha(sha1, Arrays.asList(key), Arrays.asList(arg));
  p.incr(key);
  Response<Object> result1 = p.evalsha(sha1, Arrays.asList(key), Arrays.asList(arg));
  Response<String> result2 = p.get(key);
  p.sync();

  assertNull(result0.get());
  assertNull(result1.get());
  assertEquals("13", result2.get());
}
 
Example 7
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvalshaKeyAndArgWithBinary() {
  byte[] bKey = SafeEncoder.encode("test");
  byte[] bArg = SafeEncoder.encode("3");
  String script = "redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])";
  byte[] bScript = SafeEncoder.encode(script);
  byte[] bSha1 = jedis.scriptLoad(bScript);

  assertTrue(jedis.scriptExists(bSha1) == 1);

  Pipeline p = jedis.pipelined();
  p.set(bKey, SafeEncoder.encode("0"));
  Response<Object> result0 = p.evalsha(bSha1, Arrays.asList(bKey), Arrays.asList(bArg));
  p.incr(bKey);
  Response<Object> result1 = p.evalsha(bSha1, Arrays.asList(bKey), Arrays.asList(bArg));
  Response<byte[]> result2 = p.get(bKey);
  p.sync();

  assertNull(result0.get());
  assertNull(result1.get());
  assertArrayEquals(SafeEncoder.encode("13"), result2.get());
}
 
Example 8
Source File: JedisTest.java    From spring-redis-plugin with Apache License 2.0 6 votes vote down vote up
private void jedisIncr(String key, int times) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Pipeline pipeline = jedis.pipelined();
        pipeline.set(key, "1");
        for (int i = 0; i < times; i++) {
            pipeline.incr(key);
        }
        Response<String> response = pipeline.get(key);
        pipeline.sync();
        LOGGER.info(response.get());
        jedis.del(key);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        release(jedis);
    }
}
 
Example 9
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void pipeline() throws UnsupportedEncodingException {
  Pipeline p = jedis.pipelined();
  p.set("foo", "bar");
  p.get("foo");
  List<Object> results = p.syncAndReturnAll();

  assertEquals(2, results.size());
  assertEquals("OK", results.get(0));
  assertEquals("bar", results.get(1));

}
 
Example 10
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 11
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void canRetrieveUnsetKey() {
  Pipeline p = jedis.pipelined();
  Response<String> shouldNotExist = p.get(UUID.randomUUID().toString());
  p.sync();
  assertNull(shouldNotExist.get());
}
 
Example 12
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void piplineWithError() {
  Pipeline p = jedis.pipelined();
  p.set("foo", "bar");
  Response<Set<String>> error = p.smembers("foo");
  Response<String> r = p.get("foo");
  p.sync();
  try {
    error.get();
    fail();
  } catch (JedisDataException e) {
    // that is fine we should be here
  }
  assertEquals(r.get(), "bar");
}
 
Example 13
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 14
Source File: JedisServiceDemo.java    From spring-redis-plugin with Apache License 2.0 5 votes vote down vote up
@Redis
public void incr(String key, int times) {
    Pipeline pipeline = jedis.pipelined();
    pipeline.set(key, "1");
    for (int i = 0; i < times; i++) {
        pipeline.incr(key + i);
    }
    Response<String> response = pipeline.get(key + 1);
    pipeline.sync();
    LOGGER.info(response.get());
    jedis.del(key);
}
 
Example 15
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);
}