Question

i am new in redis . I want to know how to get multiple field's value from multiple sets in redis. is it possible in Redis . and if possible then how. Example

HSET myhash1 field1 "Hello"
HSET myhash2 field2 "Hello"
HSET myhash3 field3 "Hello"
    I want to get all the three field's value from different sets at a time?
Was it helpful?

Solution

You can use Lua Scripting for this.

Example:

local a={}
a[1] = redis.call('hget',KEYS[1], KEYS[2])
a[2] = redis.call('hget',KEYS[3], KEYS[4])
a[3] = redis.call('hget',KEYS[5], KEYS[6])
return a

Coded for simplicity, ofcourse you'd make the parameters more flexible. You can also use cjson for input as well as output (if you keep in mind that serialisation eats up a tiny bit of CPU).

Another way is using pipelining, but I guess your question is about doing this atomically.

Hope this helps, TW

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top