redis.clients.jedis.Response Java Examples

The following examples show how to use redis.clients.jedis.Response. 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: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Boolean> set(final Map<String, Object> map) {
    Assert.notEmpty(map);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipelined = jedis.pipelined();
        final Map<String, Response<String>> responses = Maps.newHashMap();
        map.forEach((key, value) -> responses.put(key, pipelined.set(key, toJSONString(value))));
        pipelined.sync();

        final Map<String, Boolean> values = Maps.newHashMap();
        responses.forEach((key, response) -> values.put(key, isOK(response.get())));
        return values;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #2
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 #3
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Long> scard(final String... keys) {
    Assert.notEmpty(keys);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final Map<String, Response<Long>> responses = Maps.newHashMap();
        for (String key : keys) {
            responses.put(key, pipeline.scard(key));
        }

        pipeline.sync();
        final Map<String, Long> values = Maps.newHashMap();
        responses.forEach((key, response) -> values.put(key, response.get()));
        return values;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #4
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Boolean> hsetByNX(final String key, final Map<String, Object> map) {
    Assert.hasText(key);
    Assert.notEmpty(map);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final Map<String, Response<Long>> responses = Maps.newHashMap();
        for (Entry<String, Object> entry : map.entrySet()) {
            responses.put(entry.getKey(), pipeline.hsetnx(key, entry.getKey(), toJSONString(entry.getValue())));
        }

        pipeline.sync();
        final Map<String, Boolean> values = Maps.newHashMap();
        responses.forEach((field, response) -> values.put(field, isSuccess(response.get())));
        return values;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #5
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Boolean> setByNX(final Map<String, Object> map) {
    Assert.notEmpty(map);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipelined = jedis.pipelined();
        final Map<String, Response<Long>> responses = Maps.newHashMap();
        for (Entry<String, Object> entry : map.entrySet()) {
            responses.put(entry.getKey(), pipelined.setnx(entry.getKey(), toJSONString(entry.getValue())));
        }

        pipelined.sync();
        final Map<String, Boolean> values = Maps.newHashMap();
        responses.forEach((key, response) -> values.put(key, isSuccess(response.get())));
        return values;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #6
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 #7
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 #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 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 #10
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public boolean setByNX(final String key, final String value, final int timeout) {
    Assert.hasText(key);
    Assert.hasText(value);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final Response<Long> set = pipeline.setnx(key, value);
        final Response<Long> expire = pipeline.expire(key, timeout);
        pipeline.sync();
        return isSuccess(set.get()) && isSuccess(expire.get());
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #11
Source File: JedisPoolStream.java    From RedisDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public List<byte[]> loadFileOnce(String fileDataKey, String fileName, long blockSize) {
    Jedis jedis = jedisPool.getResource();
    Pipeline pipelined = jedis.pipelined();
    List<byte[]> res = new ArrayList<>();
    List<Response<byte[]>> temps = new ArrayList<>();
    int temp = 0;
    while (temp < blockSize) {
        Response<byte[]> data = pipelined.hget(fileDataKey.getBytes(), getBlockName(fileName, temp));
        temps.add(data);
        if (temp % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
            pipelined = jedis.pipelined();
            temps.clear();
        }
        temp++;
    }
    try {
        pipelined.sync();
    } catch (JedisConnectionException e) {
        log.error("pipelined = {}, blockSize = {}!", pipelined.toString(), blockSize);
        log.error("", e);
    } finally {
        jedis.close();
    }
    res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
    temps.clear();
    return res;
}
 
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: 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 #14
Source File: RedisHashPipelined.java    From yb-sample-apps with Apache License 2.0 5 votes vote down vote up
private long verifyWriteResult(final ArrayList<KeySubKey> ks,
                               final Response<String> retVal) {
  pipelinedOpResponseCallables.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      if (retVal.get() == null) {
        getRedisHashLoadGenerator().recordWriteFailure(ks);
        return 0;
      }
      getRedisHashLoadGenerator().recordWriteSuccess(ks);
      return 1;
    }
  });
  return 1;
}
 
Example #15
Source File: RedisHashPipelined.java    From yb-sample-apps with Apache License 2.0 5 votes vote down vote up
private void verifyReadBytes(final List<KeySubKey> keySubKeys,
                             final Response<List<byte[]>> resp) {
  pipelinedOpResponseCallables.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      List<byte[]> received = resp.get();
      if (received.size() != keySubKeys.size()) {
        LOG.error("Mismatch Received " + received.size() +
                  " responses for HMGET"
                  + " was expecting " + keySubKeys.size());
        return 0;
      }
      Iterator<KeySubKey> exptdIter = keySubKeys.iterator();
      Iterator<byte[]> rcvdIter = received.iterator();
      while (rcvdIter.hasNext()) {
        KeySubKey exptd = exptdIter.next();
        byte[] rcvd = rcvdIter.next();
        if (rcvd == null || !verifyRandomValue(exptd.getSubkey(), rcvd)) {
          LOG.error("Error in HMGet. for " + exptd.toString() + " got : " + rcvd);
          return 0;
        }
      }

      return 1;
    }
  });
}
 
Example #16
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 #17
Source File: RedisPipelinedKeyValue.java    From yb-sample-apps with Apache License 2.0 5 votes vote down vote up
public Response<String> doActualWrite(Key key) {
  Response<String> retVal;
  if (appConfig.valueSize == 0) {
    String value = key.getValueStr();
    retVal = getRedisPipeline().set(key.asString(), value);
  } else {
    retVal = getRedisPipeline().set(key.asString().getBytes(),
                                    getRandomValue(key));
  }
  return retVal;
}
 
Example #18
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 #19
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 5 votes vote down vote up
@Override
public List<byte[]> loadFileOnce(String fileDataKey, String fileName, long blockSize) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    List<byte[]> res = new ArrayList<>();
    List<Response<byte[]>> temps = new ArrayList<>();
    int temp = 0;
    //如果不分批次sync容易read time out和Java heap space
    while (temp < blockSize) {
        Response<byte[]> data = pipelined.hget(fileDataKey.getBytes(), getBlockName(fileName, temp));
        temps.add(data);
        if (temp % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
            temps.clear();
            pipelined = shardedJedis.pipelined();
        }
        temp++;
    }
    try {
        pipelined.sync();
    } catch (JedisConnectionException e) {
        log.error("pipelined = {}, blockSize = {}!", pipelined.toString(), blockSize);
        log.error("", e);
    } finally {
        shardedJedis.close();
    }
    res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList()));
    temps.clear();
    return res;
}
 
Example #20
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 #21
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 #22
Source File: RedisPermissionsRepository.java    From fiat with Apache License 2.0 5 votes vote down vote up
private Table<String, ResourceType, Response<Map<String, String>>> getAllFromRedis(
    Set<String> userIds) {
  if (userIds.size() == 0) {
    return HashBasedTable.create();
  }

  try {
    final Table<String, ResourceType, Response<Map<String, String>>> responseTable =
        ArrayTable.create(
            userIds,
            new ArrayIterator<>(
                resources.stream().map(Resource::getResourceType).toArray(ResourceType[]::new)));
    for (List<String> userIdSubset : Lists.partition(new ArrayList<>(userIds), 10)) {
      redisClientDelegate.withMultiKeyPipeline(
          p -> {
            for (String userId : userIdSubset) {
              resources.stream()
                  .map(Resource::getResourceType)
                  .forEach(
                      r -> {
                        responseTable.put(userId, r, p.hgetAll(userKey(userId, r)));
                      });
            }
            p.sync();
          });
    }
    return responseTable;
  } catch (Exception e) {
    log.error("Storage exception reading all entries.", e);
  }
  return null;
}
 
Example #23
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 #24
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 #25
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 #26
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalsha() {
  String script = "return 'success!'";
  String sha1 = jedis.scriptLoad(script);

  assertTrue(jedis.scriptExists(sha1));

  Pipeline p = jedis.pipelined();
  Response<Object> result = p.evalsha(sha1);
  p.sync();

  assertEquals("success!", result.get());
}
 
Example #27
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
@Override
public ActionCacheScanResult scanActionCache(String scanToken, int count) throws IOException {
  final String jedisScanToken = scanToken == null ? SCAN_POINTER_START : scanToken;

  ImmutableList.Builder<Map.Entry<ActionKey, String>> results = new ImmutableList.Builder<>();

  ScanParams scanParams =
      new ScanParams().match(config.getActionCachePrefix() + ":*").count(count);

  String token =
      client.call(
          jedis -> {
            ScanResult<String> scanResult = jedis.scan(jedisScanToken, scanParams);
            List<String> keyResults = scanResult.getResult();

            List<Response<String>> actionResults = new ArrayList<>(keyResults.size());
            JedisClusterPipeline p = jedis.pipelined();
            for (int i = 0; i < keyResults.size(); i++) {
              actionResults.add(p.get(keyResults.get(i)));
            }
            p.sync();
            for (int i = 0; i < keyResults.size(); i++) {
              String json = actionResults.get(i).get();
              if (json == null) {
                continue;
              }
              String key = keyResults.get(i);
              results.add(
                  new AbstractMap.SimpleEntry<>(
                      DigestUtil.asActionKey(DigestUtil.parseDigest(key.split(":")[1])), json));
            }
            String cursor = scanResult.getCursor();
            return cursor.equals(SCAN_POINTER_START) ? null : cursor;
          });
  return new ActionCacheScanResult(
      token,
      Iterables.transform(
          results.build(),
          (entry) ->
              new AbstractMap.SimpleEntry<>(
                  entry.getKey(), parseActionResult(entry.getValue()))));
}
 
Example #28
Source File: ApplicationRatelimitInterceptor.java    From moon-api-gateway with MIT License 4 votes vote down vote up
@Override
public boolean setCondition(Map<String, Response<Long>> storedValue) {
    return storedValue.get(Constant.REDIS_APP_RATELIMIT_MINUTELY).get() >= 0 && storedValue.get(Constant.REDIS_APP_RATELIMIT_DAILY).get() >= 0;
}
 
Example #29
Source File: RedisPipelinedKeyValue.java    From yb-sample-apps with Apache License 2.0 4 votes vote down vote up
public Response<String> doActualReadString(Key key) {
  return getRedisPipeline().get(key.asString());
}
 
Example #30
Source File: RedisEventLog.java    From concursus with MIT License 4 votes vote down vote up
private Response<Long> writeEventsForId(Pipeline pipeline, AggregateId id, List<Event> eventsForId) {
    return pipeline.sadd(id.toString(), serialiseEvents(eventsForId));
}