java Redis List,弹出而不删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10882713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redis List, pop without removing
提问by Zhivko Draganov
I'm using RedisTemplate(from Spring) in my Java app. I need to do pop from a list of elements correspondenting for values, but without removing it. Any suggestions?
我在我的 Java 应用程序中使用 RedisTemplate(来自 Spring)。我需要从对应于值的元素列表中弹出,但不删除它。有什么建议?
回答by Didier Spezia
You can easily peek at an item rather than popping it by using the range command.
您可以使用 range 命令轻松查看项目,而不是弹出它。
With Spring, from a RedisTemplate instance, you can get a ListOperations instance by using the opsForList() method, and then:
使用 Spring,从 RedisTemplate 实例,您可以使用 opsForList() 方法获取 ListOperations 实例,然后:
listOp.range(key, 0, 0) will return the first (left) item without popping it
listOp.range(key, -1, -1) will return the last (right) item without popping it
listOp.range(key, 0, 0) 将返回第一个(左)项而不弹出它
listOp.range(key, -1, -1) 将返回最后一个(右)项目而不弹出它
See documentation at:
请参阅以下位置的文档:
回答by nuxibyte
Not sure how to do this using RedisTemplate but to get a value from a list you can use the redis command:
不确定如何使用 RedisTemplate 执行此操作,但要从列表中获取值,您可以使用 redis 命令:
LRANGE <LIST> 0 0
LRANGE <列表> 0 0
to get the first value, where <LIST> is the name of your list.
获取第一个值,其中 <LIST> 是您的列表名称。
Is there something similar to this in RedisTemplate?
RedisTemplate 中是否有类似的东西?
回答by diyism
Is there any method in Redis to pop an item without removing it but keep it hibernate in an expire period? After the expire period (and it not deleted), this item wake up and can pop again.
Redis 中是否有任何方法可以在不删除项目的情况下弹出项目但在到期期间保持休眠状态?过期后(并且它没有被删除),这个项目会被唤醒并可以再次弹出。
http://redis.io/commands/rpoplpush
http://redis.io/commands/rpoplpush
Pattern: Reliable queue Redis is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks. A simple form of queue is often obtained pushing values into a list in the producer side, and waiting for this values in the consumer side using RPOP (using polling), or BRPOP if the client is better served by a blocking operation. However in this context the obtained queue is not reliable as messages can be lost, for example in the case there is a network problem or if the consumer crashes just after the message is received but it is still to process. RPOPLPUSH (or BRPOPLPUSH for the blocking variant) offers a way to avoid this problem: the consumer fetches the message and at the same time pushes it into a processing list. It will use the LREM command in order to remove the message from the processing list once the message has been processed. An additional client may monitor the processing list for items that remain there for too much time, and will push those timed out items into the queue again if needed.
模式:可靠队列Redis通常用作消息服务器来实现后台作业或其他类型的消息任务的处理。队列的简单形式通常是通过将值推送到生产者端的列表中来获得,并在消费者端使用 RPOP(使用轮询)等待此值,如果客户端通过阻塞操作更好地服务,则使用 BRPOP。然而,在这种情况下,获得的队列并不可靠,因为消息可能会丢失,例如在出现网络问题的情况下,或者如果消费者在收到消息后立即崩溃但仍在处理中。RPOPLPUSH(或用于阻塞变体的 BRPOPLPUSH)提供了一种避免此问题的方法:消费者获取消息,同时将其推送到处理列表中。一旦消息被处理,它将使用 LREM 命令从处理列表中删除该消息。额外的客户端可能会监视处理列表中停留时间过长的项目,并在需要时将这些超时的项目再次推送到队列中。
回答by vacing
I recommend rpoplpush, pop an item from list and lpush it to another(also can be the same) key, very useful. It can be used as a reliable queue or a round list. Here is the official document: https://redis.io/commands/rpoplpush
我推荐rpoplpush,从列表中弹出一个项目并将其 lpush 到另一个(也可以是相同的)键,非常有用。它可以用作可靠队列或轮列表。这里是官方文档:https: //redis.io/commands/rpoplpush