gpt4 book ai didi

redis - 使用redis的 'keys'命令时如何获取键和值

转载 作者:IT王子 更新时间:2023-10-29 06:11:38 27 4
gpt4 key购买 nike

我想同时获得键和值。现在我这样做:

Set<String> keys = redisTemplate.keys("Tom*");
if (keys != null) {

//get them one by one
for (String key : keys) {
String value = redisTemplate.opsForValue().get(key);
}
}

首先,我必须获取所有以“abc”开头的键。其次,我一个一个地获取值。

我可以同时获取键和值吗?

更新:

谢谢soveran。
我有一些与每个用户相关的属性:

1)Tom.loginTimes=3  
2)Tom.tradeMoneyCount=100

在我定义两个单独的键之前:Tom.loginTimes 和 Tom.tradeMoneyCount。现在我想我应该使用 hmset:

10.75.201.3:63790> hmset Tom loginTimes 3 tradeMoneyCount 100
OK
10.75.201.3:63790> hgetall Tom
1) "loginTimes"
2) "3"
3) "tradeMoneyCount"
4) "100"

谢谢。

最佳答案

hashes是正确的方法。

至于keys命令,它被添加到 redis 用于调试目的,从未打算在生产中使用。这是来自 redis docs for keys command 的警告:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

关于redis - 使用redis的 'keys'命令时如何获取键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com