简体   繁体   English

Redigo:如何使用 Golang 从 Redis 获取键值 map?

[英]Redigo: How can I get a key-value map from Redis using Golang?

Let's say I have this set of keys in my Redis:假设我的 Redis 中有这组键:

KEY           VALUE
"agent_100"   "{name: Agent1, status:online}"
"agent_200"   "{name: Agent2, status:offline}"
"agent_300"   "{name: Agent3, status:online}"
"agent_400"   "{name: Agent4, status:offline}"

I need to return a map with all those keys and values in Golang using Redigo.我需要使用 Redigo 返回一个 map 以及 Golang 中的所有这些键和值。 The output would be something like a map[uint64]string with this key-values: output 类似于具有以下键值的map[uint64]string

100 -> "{name: Agent1, status:online}"
200 -> "{name: Agent2, status:offline}"
300 -> "{name: Agent3, status:online}"
400 -> "{name: Agent4, status:offline}"

If I do a Scan I can get all the keys matching a pattern like agent_* and maybe then I can do a MGET with all those keys to get the values, but how I can link those keys and values in a simple way?如果我进行Scan ,我可以获得与agent_*之类的模式匹配的所有键,然后我可以使用所有这些键执行MGET以获取值,但是如何以简单的方式链接这些键和值?

There's no a library function to get not only the keys that match a pattern but also the values so I can return a map with that?没有库 function 不仅可以获取与模式匹配的键,还可以获取值,因此我可以返回 map 吗?

I'm using redigo now but I was also looking into go-redis to see if there is a simpler way to achieve this, I'm open to consider other options.我现在正在使用redigo ,但我也在研究go-redis以查看是否有更简单的方法来实现这一点,我愿意考虑其他选择。

Thanks!谢谢!

MGET preserves the order. MGET 保留订单。 So if you send in a list of keys, you should expect the results in the same order, with missing keys containing nil.因此,如果您发送一个键列表,您应该期望结果的顺序相同,缺少的键包含 nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM