1

I want to get both keys and values . Now I'm doing it like this:

Set<String> keys = redisTemplate.keys("Tom*");
if (keys != null) {
 //get them one by one
 for (String key : keys) {
 String value = redisTemplate.opsForValue().get(key);
 }
}

first, I have to get all the keys which starts with "abc". second, I get values one by one.

Can I get both keys and values at one time?

UPDATE:

Thank soveran.
I have some properties associated to each user:

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

Before I define two separated keys: Tom.loginTimes and Tom.tradeMoneyCount. Now I think I should use 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"

Thanks.

Leonid Beschastny
51.6k10 gold badges121 silver badges126 bronze badges
asked Apr 9, 2015 at 6:12
4
  • You can't, but also make sure you read the "Warning" note in the documentation for the KEYS command: redis.io/commands/keys Commented Apr 9, 2015 at 6:39
  • Can you describe your use case? Maybe we can come up with a different solution. Commented Apr 9, 2015 at 6:40
  • we want something like: select * from table_name where column_name like 'abc*'. Now I'm wondering how can I do this in redis. Commented Apr 9, 2015 at 8:08
  • Thank soveran. I have some properties associated to each user: 1) Tom.loginTimes=3 2)Tom.tradeMoneyCount=100. Before I define two separate keys: Tom.loginTimes and Tom.tradeMoneyCount. Now I think I should use hmset: hmset Tom loginTimes 3 tradeMoneyCount 100. Thanks. Commented Apr 9, 2015 at 8:50

2 Answers 2

1

hashes is the right way to do it.

As for the 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.

answered Apr 9, 2015 at 9:17
Sign up to request clarification or add additional context in comments.

1 Comment

but use spring-data's RedisTemplate to 'hgetall' is a litte weird:Map<Object, Object> entries = redisTemplate.boundHashOps(name).entries(); see:stackoverflow.com/questions/17230205/…
1

You can use below kind of code to get all the keys at the same time, and it returns a set of keys. I am using the 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;
 }
Nathaniel Ford
21.3k20 gold badges98 silver badges112 bronze badges
answered Sep 8, 2015 at 21:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.