使用redis的'keys'命令时如何同时获取键和值
how to get both keys and values when using redis's 'keys' command
我想同时获取键和值。现在我是这样做的:
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 command, it was added to redis for debug purposes and never meant to be used in production. Here is a warning from 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.
您可以使用以下代码同时获取所有密钥,并且returns 一组密钥。我正在使用 Spring Redis API:
public StringBuffer getAllKeys() {
System.out.println("get all keys");
StringBuffer sb = new StringBuffer();
Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while(it.hasNext()){
byte[] data = (byte[])it.next();
sb.append(new String(data, 0, data.length));
}
return sb;
}
我想同时获取键和值。现在我是这样做的:
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 command, it was added to redis for debug purposes and never meant to be used in production. Here is a warning from 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.
您可以使用以下代码同时获取所有密钥,并且returns 一组密钥。我正在使用 Spring Redis API:
public StringBuffer getAllKeys() {
System.out.println("get all keys");
StringBuffer sb = new StringBuffer();
Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while(it.hasNext()){
byte[] data = (byte[])it.next();
sb.append(new String(data, 0, data.length));
}
return sb;
}