Redis 在不进行迭代和弹出的情况下获取列表的所有值

我有一个简单的 redis 列表 key = > “ Supply _ id”

现在我只需要它检索 list 的所有值,而不需要实际迭代或弹出 list 中的值

从列表中检索所有值的示例现在我已经遍历了 Redis 长度

element = []
0.upto(redis.llen("supplier_id")-1) do |index|
element << redis.lindex("supplier_id",index)
end

这可以做 没有迭代也许更好的重建模型。可以有人建议

84722 次浏览

I'm a bit unclear on your question but if the supplier_id is numeric, why not use a ZSET?

Add your values like so:

ZADD suppliers 1 "data for supplier 1"
ZADD suppliers 2 "data for supplier 2"
ZADD suppliers 3 "data for supplier 3"

You could then remove everything up to (but not including supplier three) like so:

ZREMRANGEBYSCORE suppliers -inf 2

or

ZREMRANGEBYSCORE suppliers -inf (3

That also gives you very fast access (by supplier id) if you just want to read from it.

Hope that helps!

To retrieve all the items of a list with Redis, you do not need to iterate and fetch each individual items. It would be really inefficient.

You just have to use the LRANGE command to retrieve all the items in one shot.

elements = redis.lrange( "supplier_id", 0, -1 )

will return all the items of the list without altering the list itself.