22

How do I get the value of multiple keys from redis using a sorted set?

zadd Users 0 David
zadd Users 5 John
zadd Users 15 Linda
zrevrange Users 0 -1 withscores

This will have two users in it.

How can I retrieve the users with key 'David' and 'Linda' in one query?

4 Answers 4

35

You can use Redis MGET

redis> MGET key1 key2 nonexisting
1) "Hello"
2) "World"
3) (nil)

More here http://redis.io/commands/mget

Sign up to request clarification or add additional context in comments.

Comments

12

There are multiple ways to do it without introducing a new command in Redis.

For instance, you can fill a temporary set with the names you are interested in, then calculate the intersection between the temporary set and the zset:

multi
  sadd tmp David Linda ... and more ...
  zinterstore res 2 tmp Users weights 0 1
  zrange res 0 -1 withscores
  del tmp res
exec

With pipelining, this will only generate one roundtrip and you can fill an arbitrary number of input parameters in tmp.

With Redis 2.6, you can also wrap these lines into a server-side Lua script to finally get a command accepting an input list and returning the result you want:

eval "redis.call( 'sadd', 'tmp', unpack(KEYS) );
      redis.call( 'zinterstore', 'res', 2, 'tmp', 'Users', 'weights', 0, 1 );
      local res = redis.call( 'zrange', 'res', 0, -1, 'withscores' );
      redis.call( 'del', 'res', 'tmp' ) ; 
      return res
     " 2 David Linda

You can safely assume no new command will be added to Redis if it can easily been implemented using scripting.

1 Comment

How do you enter multi-line lua scripts in the shell? I entered eval "redis.call( 'sadd', 'tmp', unpack(KEYS) );, hit ENTER, then got an error (invalid arguments); do I need a special character or something at the end to enter the rest of the script?
1

One uses a sorted set because you want to deal with items that are sorted. What you are asking for is to not use a sorted set as a sorted set. If you don't care about sort order, then perhaps a sorted set is not what you are looking for. You already can retrieve multiple keys, but not arbitrary ones.

If your primary goal is to retrieve multiple arbitrary keys, use a hash and hmget. If your primary need is to access a sorted set, use sorted set and either go the scripting route or pipeline a series of zscore calls.

Comments

0

You cannot get this with one command. The closest you can do to get it in one response:

MULTI
ZSCORE Users David
ZSCORE Users Linda
EXEC

EDIT: Alternatively, you can maintain a parallel hash with users' scores, and query it with

HMGET UserScores David Linda

3 Comments

so keeping a hash map and a sorted set is a better way you say?
No, it's not "better". It's just a way to achieve what you want in one query.
I see. This might be a good improvement to redis I guess. Being able to retrieve the value of multiple keys in one query.

Your Answer

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.