Anake
Anake

Reputation: 7649

Select certain hash elements using values of a set in Redis

I'm new to Redis but would like to achieve the following as fast as possible.

I have a set of ids for each country like the following:

SADD country:uk 1 2
SADD country:de 5 6

I have a hash with timestamp and the ids from the country set e.g.

HSET ts1:1 x 1 y 2
HSET ts1:2 x 3 y 4
HSET ts1:3 x 3 y 4

I would like to use the country:uk key to retrieve just the values of the ts1:1 and ts1:2 hashes.

I get that I can return the ids in the country set to my programming language and then contruct the calls to retrieve the values, but was hoping there was a way to directly do this in Redis without all the network load. Perhaps using lua scripting.

Update: Updated example to be closer to what I'm trying to achieve

Upvotes: 0

Views: 467

Answers (1)

for_stack
for_stack

Reputation: 22886

Yes, as you mentioned, you can wrap the logic into Lua scripting, which can reduce the network load.

However, if the fields of your HASH are fixed, or you only have limited number of fields, you can use the SORT command to achieve your goal.

SORT country:uk BY NOSORT GET ts1:*->field1 GET ts1:*->field2

Upvotes: 1

Related Questions