Redis 是否有可能在一个查询中处理 ids 集合

is redis has possible to process the ids collection in one query

现在我正在存储一个包含文章 ID 的列表数据,现在我正在使用 lrange 来获取文章 ID 列表。然后第二个我想从redis中查询文章(我也将热门文章缓存到redis中),有没有办法使用in query with article id?或获取这样的文章:

for(long id in ids){
   redis.get(id)
}

这种方式可能会访问redis n次,一次获取所有article可能是最好的方式。像这样:

redis.get(ids)

只能访问一次。

您可以使用 MGET 获取键列表的值,如果未找到键,它 return 为 null。

例如

MGET article_1 article_2 article_3 article_4 将return 4篇同顺序

Output: Article1, Article2, Article3, Article4

例如,如果缺少 article_3,它仍然会 return 4 个项目,但第 3 个项目将为空。

Output: Article1, Article2, null, Article4

MGET key [key ...] Available since 1.0.0.

Time complexity: O(N) where N is the number of keys to retrieve.

Returns the values of all specified keys. For every key that does not hold a string value or does not exist, the special value nil is returned. Because of this, the operation never fails.

https://redis.io/commands/mget