本文整理了Java中redis.clients.jedis.Jedis.blpop()
方法的一些代码示例,展示了Jedis.blpop()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jedis.blpop()
方法的具体详情如下:
包路径:redis.clients.jedis.Jedis
类名称:Jedis
方法名:blpop
[英]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<String> execute(Jedis connection) {
return connection.blpop(timeout, keys);
}
}.run(keys.length, keys);
代码示例来源:origin: sohutv/cachecloud
@Override
public List<byte[]> execute(Jedis connection) {
return connection.blpop(timeout, keys);
}
}.runBinary(keys.length, keys);
代码示例来源:origin: sohutv/cachecloud
@Override
public List<String> execute(Jedis connection) {
return connection.blpop(timeout, key);
}
}.run(key);
代码示例来源:origin: sohutv/cachecloud
public List<byte[]> execute(Jedis connection) {
return connection.blpop(keyByte);
}
}.runBinary(keyByte);
代码示例来源:origin: sohutv/cachecloud
@Override
public List<String> blpop(int timeout, String key) {
return blpop(key, String.valueOf(timeout));
}
代码示例来源:origin: sohutv/cachecloud
@Override
public List<String> blpop(int timeout, String key) {
Jedis j = getShard(key);
return j.blpop(timeout, key);
}
代码示例来源:origin: sohutv/cachecloud
public List<String> blpop(String arg) {
Jedis j = getShard(arg);
return j.blpop(arg);
}
代码示例来源:origin: sohutv/cachecloud
public List<byte[]> blpop(byte[] arg) {
Jedis j = getShard(arg);
return j.blpop(arg);
}
代码示例来源:origin: Netflix/conductor
@Override
@Deprecated
public List<String> blpop(String arg) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.blpop(arg);
} finally {
if (jedis != null)
jedis.close();
}
}
代码示例来源:origin: Netflix/conductor
@Override
public List<String> blpop(int timeout, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.blpop(timeout, key);
} finally {
if (jedis != null)
jedis.close();
}
}
代码示例来源:origin: sohutv/cachecloud
return blpop(getArgsAddTimeout(timeout, keys));
代码示例来源:origin: jfinal/jfinal
/**
* BLPOP 是列表的阻塞式(blocking)弹出原语。
* 它是 LPOP 命令的阻塞版本,当给定列表内没有任何元素可供弹出的时候,连接将被 BLPOP 命令阻塞,直到等待超时或发现可弹出元素为止。
* 当给定多个 key 参数时,按参数 key 的先后顺序依次检查各个列表,弹出第一个非空列表的头元素。
*
* 参考:http://redisdoc.com/list/blpop.html
* 命令行:BLPOP key [key ...] timeout
*/
@SuppressWarnings("rawtypes")
public List blpop(int timeout, Object... keys) {
Jedis jedis = getJedis();
try {
List<byte[]> data = jedis.blpop(timeout, keysToBytesArray(keys));
return keyValueListFromBytesList(data);
}
finally {close(jedis);}
}
代码示例来源:origin: spring-projects/spring-data-redis
@Override
public List<byte[]> bLPop(int timeout, byte[]... keys) {
Assert.notNull(keys, "Key must not be null!");
Assert.noNullElements(keys, "Keys must not contain null elements!");
if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) {
try {
return connection.getCluster().blpop(timeout, keys);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
return connection.getClusterCommandExecutor()
.executeMultiKeyCommand(
(JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.blpop(timeout, key),
Arrays.asList(keys))
.getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList());
}
代码示例来源:origin: spring-projects/spring-data-redis
@Override
public List<byte[]> bLPop(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().blpop(bXPopArgs(timeout, keys))));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().blpop(bXPopArgs(timeout, keys))));
return null;
}
return connection.getJedis().blpop(timeout, keys);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
代码示例来源:origin: io.leopard/leopard-redis
@SuppressWarnings("deprecation")
@Override
public List<String> blpop(String arg) {
return jedis.blpop(arg);
}
代码示例来源:origin: io.leopard/leopard-redis
@SuppressWarnings("deprecation")
@Override
public List<String> blpop(String arg) {
return jedis.blpop(arg);
}
代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis
@Override
public List<String> blpop(String... args) {
String command = "blpop";
return instrumented(command, () -> delegated.blpop(args));
}
代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis
@Override
public List<byte[]> blpop(int timeout, byte[]... keys) {
String command = "blpop";
return instrumented(command, () -> delegated.blpop(timeout, keys));
}
代码示例来源:origin: org.nutz/nutz-integration-jedis
public List<byte[]> blpop(byte[]... args) {
Jedis jedis = getJedis();
try {
return jedis.blpop(args);
} finally {Streams.safeClose(jedis);}
}
代码示例来源:origin: com.netflix.conductor/conductor-redis-persistence
@Override
public List<String> blpop(int timeout, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.blpop(timeout, key);
} finally {
if (jedis != null)
jedis.close();
}
}
内容来源于网络,如有侵权,请联系作者删除!