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

The following examples show how to use redis.clients.jedis.Pipeline#set() . 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: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Pipelining
 * 测试时间:0.287 seconds
 */
@Test
@Ignore
public void testPipelining(){
	Jedis jedis = new Jedis("localhost");
	Pipeline pipeline = jedis.pipelined();
	long start = System.currentTimeMillis();
	for(int i = 0; i< COUNTER; i++){
		pipeline.set("p" + i, "p" + i);
		if(i == 100){
			System.out.println(jedis.get("p1"));
		}
	}
	List<Object> results = pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	logger.info("Pipelined SET: " + ((end - start)/1000.0) + " seconds");
	jedis.close();
	System.out.println("result: " + results.get(0));
	System.out.println(jedis.get("p1"));
}
 
Example 2
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 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: 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 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: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testCombPipelineTrans() {// 0.099秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Pipeline pipeline = jedis.pipelined();
	pipeline.multi();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.exec();
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 7
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 8
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisDataException.class)
public void testJedisThowExceptionWhenInPipeline() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.set("foo", "3");
  jedis.get("somekey");
  fail("Can't use jedis instance when in Pipeline");
}
 
Example 9
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 10
Source File: JedisTest.java    From gameserver with Apache License 2.0 5 votes vote down vote up
public void testLongSetPerformance1_2() {
	JedisAdapter jedis = new JedisAdapter(host, port, config);

	//Store a long list
	int count = 500000;
	Set<byte[]> userList = jedis.keys("key_*".getBytes());
	for ( byte[] key : userList ) {
		jedis.del(key);
	}
	
	byte[][] longList = createLongList(count);
	Pipeline pipeline = jedis.pipelined();
	for ( int i=0; i<longList.length; i++ ) {
		pipeline.set(("key_"+r.nextInt()).getBytes(), longList[i]);
	}
	pipeline.sync();
	userList = jedis.keys("key_*".getBytes());
	assertTrue(userList.size()>0);
	
	//Getting the list.
	long startM = 0l, endM = 0l;
	startM = System.currentTimeMillis();
	int max=10;
	for ( int i=0; i<max; i++ ) {
		userList = jedis.keys("key_*".getBytes());
	}
	endM = System.currentTimeMillis();
	System.out.println("Redis keypattern:"+ userList.size() + " loop " + max + " perform: " + (endM-startM)/max);
}
 
Example 11
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 12
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void setUnreadCount(FeedSubscription sub, UnreadCount count) {
	try (Jedis jedis = pool.getResource()) {
		String key = buildRedisUnreadCountKey(sub);

		Pipeline pipe = jedis.pipelined();
		pipe.del(key);
		pipe.set(key, MAPPER.writeValueAsString(count));
		pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
		pipe.sync();
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 13
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 14
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisDataException.class)
public void pipelineMultiShoudThrowJedisDataExceptionWhenAlreadyInMulti() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.multi();
  pipeline.set("foo", "3");
  pipeline.multi();
}
 
Example 15
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 16
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 17
Source File: TransactionCommandsTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testResetStateWhenInMultiWithinPipeline() {
  jedis.auth("foobared");

  Pipeline p = jedis.pipelined();
  p.multi();
  p.set("foooo", "barrr");

  jedis.resetState();
  assertEquals(null, jedis.get("foooo"));
}
 
Example 18
Source File: SomeOperate.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
public static void testPipeLineAndNormal(Jedis jedis)
		throws InterruptedException {
	Logger logger = Logger.getLogger("javasoft");
	long start = System.currentTimeMillis();
	for (int i = 0; i < 10000; i++) {
		jedis.set(String.valueOf(i), String.valueOf(i));
	}
	long end = System.currentTimeMillis();
	logger.info("the jedis total time is:" + (end - start));

	Pipeline pipe = jedis.pipelined();// �ȴ���һ��pipeline�����Ӷ���
	long start_pipe = System.currentTimeMillis();
	for (int i = 0; i < 10000; i++) {
		pipe.set(String.valueOf(i), String.valueOf(i));
	}
	pipe.sync();// ��ȡ���е�response
	long end_pipe = System.currentTimeMillis();
	logger.info("the pipe total time is:" + (end_pipe - start_pipe));
	
	BlockingQueue<String> logQueue = new LinkedBlockingQueue<String>();
	long begin = System.currentTimeMillis();
	for (int i = 0; i < 10000; i++) {
		logQueue.put("i=" + i);
	}
	long stop = System.currentTimeMillis();
	logger.info("the BlockingQueue total time is:" + (stop - begin));
}
 
Example 19
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void setUserRootCategory(User user, Category category) {
	try (Jedis jedis = pool.getResource()) {
		String key = buildRedisUserRootCategoryKey(user);

		Pipeline pipe = jedis.pipelined();
		pipe.del(key);
		pipe.set(key, MAPPER.writeValueAsString(category));
		pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
		pipe.sync();
	} catch (JsonProcessingException e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 20
Source File: RedisEasyTest.java    From javabase with Apache License 2.0 4 votes vote down vote up
private static void pipleWithTransation(Pipeline pipeline) {
    for (int i = 0; i < KEY_COUNT; i++) {
        pipeline.set("pipletransation" + i, i + "");
    }
    pipeline.exec();
}