redis.clients.jedis.Jedis.brpop()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(235)

本文整理了Java中redis.clients.jedis.Jedis.brpop()方法的一些代码示例,展示了Jedis.brpop()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jedis.brpop()方法的具体详情如下:
包路径:redis.clients.jedis.Jedis
类名称:Jedis
方法名:brpop

Jedis.brpop介绍

[英]BLPOP (and BRPOP) is a blocking list pop primitive. You can see this commands as blocking versions of LPOP and RPOP able to block if the specified keys don't exist or contain empty lists.

The following is a description of the exact semantic. We describe BLPOP but the two commands are identical, the only difference is that BLPOP pops the element from the left (head) of the list, and BRPOP pops from the right (tail).

Non blocking behavior

When BLPOP is called, if at least one of the specified keys contain a non empty list, an element is popped from the head of the list and returned to the caller together with the name of the key (BLPOP returns a two elements array, the first element is the key, the second the popped value).

Keys are scanned from left to right, so for instance if you issue BLPOP list1 list2 list3 0 against a dataset where list1 does not exist but list2 and list3 contain non empty lists, BLPOP guarantees to return an element from the list stored at list2 (since it is the first non empty list starting from the left).

Blocking behavior

If none of the specified keys exist or contain non empty lists, BLPOP blocks until some other client performs a LPUSH or an RPUSH operation against one of the lists.

Once new data is present on one of the lists, the client finally returns with the name of the key unblocking it and the popped value.

When blocking, if a non-zero timeout is specified, the client will unblock returning a nil special value if the specified amount of seconds passed without a push operation against at least one of the specified keys.

The timeout argument is interpreted as an integer value. A timeout of zero means instead to block forever.

Multiple clients blocking for the same keys

Multiple clients can block for the same key. They are put into a queue, so the first to be served will be the one that started to wait earlier, in a first-blpopping first-served fashion.

blocking POP inside a MULTI/EXEC transaction

BLPOP and BRPOP can be used with pipelining (sending multiple commands and reading the replies in batch), but it does not make sense to use BLPOP or BRPOP inside a MULTI/EXEC block (a Redis transaction).

The behavior of BLPOP inside MULTI/EXEC when the list is empty is to return a multi-bulk nil reply, exactly what happens when the timeout is reached. If you like science fiction, think at it like if inside MULTI/EXEC the time will flow at infinite speed :)

Time complexity: O(1)
[中]BLPOP(和BRPOP)是一个阻止列表pop原语。您可以将此命令视为阻止版本的LPOP和RPOP,如果指定的密钥不存在或包含空列表,则可以阻止。
下面是对确切语义的描述。我们描述了BLPOP,但这两个命令是相同的,唯一的区别是BLPOP从列表的左侧(头部)弹出元素,而BRPOP从右侧(尾部)弹出元素。
非阻塞行为
调用BLPOP时,如果指定的键中至少有一个包含非空列表,则从列表的开头弹出一个元素,并将该元素与键的名称一起返回给调用方(BLPOP返回一个两元素数组,第一个元素是键,第二个元素是弹出的值)。
键是从左到右扫描的,因此,例如,如果针对一个数据集发出BLPOP list1 list2 list3 0,其中list1不存在,但list2和list3包含非空列表,BLPOP保证从存储在list2的列表返回一个元素(因为它是从左开始的第一个非空列表)。
阻塞行为
如果指定的键不存在或不包含非空列表,BLPOP将阻塞,直到其他客户端对其中一个列表执行LPUSH或RPUSH操作。
一旦新数据出现在其中一个列表上,客户端最终返回解除阻止它的密钥的名称和弹出的值。
阻塞时,如果指定了非零超时,则如果在未对至少一个指定键执行推送操作的情况下通过了指定的秒数,则客户端将取消阻塞并返回nil特殊值。
超时参数被解释为整数值。超时为零意味着永远阻塞。
多个客户端阻塞相同的密钥
多个客户端可以为同一个密钥进行阻止。他们被排成一个队列,所以第一个被服务的人将是以先到先得的方式开始等待的人。
阻止MULTI/EXEC事务中的POP
BLPOP和BRPOP可以用于流水线(发送多个命令并批量读取回复),但在MULTI/EXEC块(Redis事务)中使用BLPOP或BRPOP没有意义。
当列表为空时,MULTI/EXEC中BLPOP的行为是返回一个MULTI-bulk nil回复,这正是达到超时时发生的情况。如果你喜欢科幻小说,就好像在MULTI/EXEC中,时间将以无限的速度流动:)
时间复杂度:O(1)

代码示例

代码示例来源:origin: sohutv/cachecloud

@Override
 public List<byte[]> execute(Jedis connection) {
  return connection.brpop(timeout, keys);
 }
}.runBinary(keys.length, keys);

代码示例来源:origin: sohutv/cachecloud

@Override
 public List<String> execute(Jedis connection) {
  return connection.brpop(timeout, key);
 }
}.run(key);

代码示例来源:origin: sohutv/cachecloud

@Override
 public List<String> execute(Jedis connection) {
  return connection.brpop(timeout, keys);
 }
}.run(keys.length, keys);

代码示例来源:origin: sohutv/cachecloud

public List<byte[]> execute(Jedis connection) {
    return connection.brpop(keyByte);
  }
}.runBinary(keyByte);

代码示例来源:origin: sohutv/cachecloud

@Override
public List<String> brpop(int timeout, String key) {
 return brpop(key, String.valueOf(timeout));
}

代码示例来源:origin: sohutv/cachecloud

@Override
public List<String> brpop(int timeout, String key) {
 Jedis j = getShard(key);
 return j.brpop(timeout, key);
}

代码示例来源:origin: sohutv/cachecloud

public List<byte[]> brpop(byte[] arg) {
 Jedis j = getShard(arg);
 return j.brpop(arg);
}

代码示例来源:origin: sohutv/cachecloud

public List<String> brpop(String arg) {
 Jedis j = getShard(arg);
 return j.brpop(arg);
}

代码示例来源:origin: Netflix/conductor

@Override
@Deprecated
public List<String> brpop(String arg) {
 Jedis jedis = null;
  try {
   jedis = jedisPool.getResource();
   return jedis.brpop(arg);
  } finally {
   if (jedis != null)
    jedis.close();
  }
}

代码示例来源:origin: Netflix/conductor

@Override
public List<String> brpop(int timeout, String key) {
 Jedis jedis = null;
  try {
   jedis = jedisPool.getResource();
   return jedis.brpop(timeout, key);
  } finally {
   if (jedis != null)
    jedis.close();
  }
}

代码示例来源:origin: sohutv/cachecloud

return brpop(getArgsAddTimeout(timeout, keys));

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public List<byte[]> bRPop(int timeout, byte[]... keys) {
  Assert.notNull(keys, "Key must not be null!");
  Assert.noNullElements(keys, "Keys must not contain null elements!");
  return connection.getClusterCommandExecutor()
      .executeMultiKeyCommand(
          (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.brpop(timeout, key),
          Arrays.asList(keys))
      .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList());
}

代码示例来源:origin: jfinal/jfinal

/**
 * BRPOP 是列表的阻塞式(blocking)弹出原语。
 * 它是 RPOP 命令的阻塞版本,当给定列表内没有任何元素可供弹出的时候,连接将被 BRPOP 命令阻塞,直到等待超时或发现可弹出元素为止。
 * 当给定多个 key 参数时,按参数 key 的先后顺序依次检查各个列表,弹出第一个非空列表的尾部元素。
 * 关于阻塞操作的更多信息,请查看 BLPOP 命令, BRPOP 除了弹出元素的位置和 BLPOP 不同之外,其他表现一致。
 * 
 * 参考:http://redisdoc.com/list/brpop.html
 * 命令行:BRPOP key [key ...] timeout
 */
@SuppressWarnings("rawtypes")
public List brpop(int timeout, Object... keys) {
  Jedis jedis = getJedis();
  try {
    List<byte[]> data = jedis.brpop(timeout, keysToBytesArray(keys));
    return keyValueListFromBytesList(data);
  }
  finally {close(jedis);}
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public List<byte[]> bRPop(int timeout, byte[]... keys) {
  Assert.notNull(keys, "Key must not be null!");
  Assert.noNullElements(keys, "Keys must not contain null elements!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().brpop(bXPopArgs(timeout, keys))));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().brpop(bXPopArgs(timeout, keys))));
      return null;
    }
    return connection.getJedis().brpop(timeout, keys);
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: io.leopard/leopard-redis

@Override
  public List<String> brpop(int timeout, String key) {

    return jedis.brpop(timeout, key);
  }
}

代码示例来源:origin: tangyanbo/springmore

@Override
  public String action(Jedis jedis) {
    List<String> nameValuePair = jedis.brpop(timeout, key);
    if (nameValuePair != null) {
      return nameValuePair.get(1);
    } else {
      return null;
    }
  }
});

代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis

@Override
@Deprecated
public List<String> brpop(String arg) {
 String command = "brpop";
 return instrumented(command, () -> delegated.brpop(arg));
}

代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis

@Override
@Deprecated
public List<byte[]> brpop(byte[] arg) {
 String command = "brpop";
 return instrumented(command, () -> delegated.brpop(arg));
}

代码示例来源:origin: org.nutz/nutz-integration-jedis

/**
 * @deprecated unusable command, this command will be removed in 3.0.0.
 */
public List<byte[]> brpop(byte[] arg) {
  Jedis jedis = getJedis();
  try {
    return jedis.brpop(arg);
  } finally {Streams.safeClose(jedis);}
}

代码示例来源:origin: org.springframework.data/spring-data-redis

@Override
public List<byte[]> bRPop(int timeout, byte[]... keys) {
  Assert.notNull(keys, "Key must not be null!");
  Assert.noNullElements(keys, "Keys must not contain null elements!");
  return connection.getClusterCommandExecutor()
      .executeMultiKeyCommand(
          (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.brpop(timeout, key),
          Arrays.asList(keys))
      .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList());
}

相关文章

微信公众号

最新文章

更多

Jedis类方法