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

The following examples show how to use redis.clients.jedis.Pipeline#syncAndReturnAll() . 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: PipelineTest.java    From code with Apache License 2.0 6 votes vote down vote up
@Test
public void testPipeline() {
    try (Jedis jedis = new Jedis("192.168.108.129", 6379)){
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            Pipeline pipeline = jedis.pipelined();
            for (int j = i * 100; j < (i + 1) * 100; j++) {
                pipeline.hset("hashKey-" + j, "field-" + j, "value-" + j);
            }
            pipeline.syncAndReturnAll();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start)); // 耗时:108
        jedis.flushAll();//清空数据库,方便后续测试
    }
}
 
Example 2
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testPipelined() {// 0.076秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Pipeline pipeline = jedis.pipelined();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	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 3
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 4
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 5
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 6
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void multi() {
  Pipeline p = jedis.pipelined();
  p.multi();
  Response<Long> r1 = p.hincrBy("a", "f1", -1);
  Response<Long> r2 = p.hincrBy("a", "f1", -2);
  Response<List<Object>> r3 = p.exec();
  List<Object> result = p.syncAndReturnAll();

  assertEquals(new Long(-1), r1.get());
  assertEquals(new Long(-3), r2.get());

  assertEquals(4, result.size());

  assertEquals("OK", result.get(0));
  assertEquals("QUEUED", result.get(1));
  assertEquals("QUEUED", result.get(2));

  // 4th result is a list with the results from the multi
  @SuppressWarnings("unchecked")
  List<Object> multiResult = (List<Object>) result.get(3);
  assertEquals(new Long(-1), multiResult.get(0));
  assertEquals(new Long(-3), multiResult.get(1));

  assertEquals(new Long(-1), r3.get().get(0));
  assertEquals(new Long(-3), r3.get().get(1));

}
 
Example 7
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Unsets a state of a given trigger key by removing the trigger from the trigger state zsets. <br>
 * The caller should handle synchronization.
 *
 * @param triggerHashKey the trigger hash key
 * @param jedis the redis client
 * @return 1- if removed, 0- if the trigger was stateless, -1- if unset state failed  
 * @throws JobPersistenceException 
 */
private long unsetTriggerState(String triggerHashKey) throws JobPersistenceException {
	Long removed = -1L;
     try (Jedis jedis = pool.getResource()) {
		Pipeline p = jedis.pipelined();
		p.zrem(RedisTriggerState.WAITING.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.PAUSED.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.BLOCKED.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.PAUSED_BLOCKED.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.ACQUIRED.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.COMPLETED.getKey(), triggerHashKey);
		p.zrem(RedisTriggerState.ERROR.getKey(), triggerHashKey);
		List<Object> results = p.syncAndReturnAll();
		
		for (Object result : results) {
			removed = (Long)result;
			if (removed == 1) {
				jedis.hset(triggerHashKey, LOCKED_BY, "");
				jedis.hset(triggerHashKey, LOCK_TIME, "");
				break;
			}
		}
	} catch (Exception ex) {
		removed = -1L;
		log.error("could not unset state for trigger: " + triggerHashKey);
		throw new JobPersistenceException(ex.getMessage(), ex.getCause());
     }
	return removed;
}
 
Example 8
Source File: RedisTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private void doBatch(Batch batch) throws StageException {
  Iterator<Record> records = batch.getRecords();
  List<ErrorRecord> tempRecord = new ArrayList<>();
  Pipeline p;

  try {
    p = jedis.pipelined();

    while (records.hasNext()) {
      Record record = records.next();
      for (RedisFieldMappingConfig parameters : conf.redisFieldMapping) {
        String key = null;
        // Special treatment is only given to deletes -
        // all other records will be handled as an upsert.
        if (OperationType.DELETE_CODE == getOperationFromHeader(record)) {
          key = getDeleteKey(record, parameters);
          doDeleteRecord(record, tempRecord, p, key);
          continue;
        }

        if (record.has(parameters.keyExpr)) {
          key = record.get(parameters.keyExpr).getValueAsString();
        }
        Field value = record.get(parameters.valExpr);

        if (key != null && value != null) {
          switch (parameters.dataType) {
            case STRING:
              doUpsertString(record, tempRecord, p, key, value);
              break;
            case LIST:
              doUpsertList(record, tempRecord, p, key, value);
              break;
            case SET:
              doUpsertSet(record, tempRecord, p, key, value);
              break;
            case HASH:
              doUpsertHash(record, tempRecord, p, key, value);
              break;
            default:
              LOG.error(Errors.REDIS_05.getMessage(), parameters.dataType);
              errorRecordHandler.onError(new OnRecordErrorException(record, Errors.REDIS_05, parameters.dataType));
              break;
          }
        } else {
          LOG.warn(Errors.REDIS_07.getMessage(), parameters.keyExpr, parameters.valExpr, record);
        }

        // set the expire time
        if (parameters.ttl > 0) {
          p.expire(key, parameters.ttl);
        }
      }
    }

    List<Object> results = p.syncAndReturnAll();

    int index = 0;
    for (Object result : results) {
      if (!("OK".equals(result) || Long.class.equals(result == null ? null : result.getClass()))) {
        LOG.error(
            Errors.REDIS_03.getMessage(),
            tempRecord.get(index).operation,
            tempRecord.get(index).key,
            tempRecord.get(index).value
        );
        errorRecordHandler.onError(new OnRecordErrorException(
            tempRecord.get(index).record,
            Errors.REDIS_03,
            tempRecord.get(index).operation,
            tempRecord.get(index).key,
            tempRecord.get(index).value,
            result.toString()
        ));
      }
      index++;
    }
    retries = 0;
  } catch (JedisException ex) {
    handleException(ex, batch, tempRecord);
  }
}