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

The following examples show how to use redis.clients.jedis.Pipeline#incr() . 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: 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 2
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 3
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 4
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 5
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 6
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);
}