Reputation: 2428
I have stored some key value pairs in redis hashes
hmset employee:1 name clarke age 24 status 1
hmset employee:2 name danne age 42 status 0
hmset employee:3 name sachin age 38 status 1
hmset employee:4 name pasty age 27 status 1
hmset employee:5 name cake age 39 status 0
hmset employee:6 name mike age 20 status 1
hmget employee:1 name age status
this gets individual employee but how can I get in range. I mean in above example there are 6 employee records. So how can I specify the limit saying I need 1 - 3 records or 3 - 6th record.
Similar to limit 2 4 in MySQL
Upvotes: 1
Views: 2277
Reputation: 10907
If you have a set of IDs, you could use SORT for it.
hmset employee:1 name clarke age 24 status 1
sadd employees 1
hmset employee:2 name danne age 42 status 0
sadd employees 2
hmset employee:3 name sachin age 38 status 1
sadd employees 3
hmset employee:4 name pasty age 27 status 1
sadd employees 4
hmset employee:5 name cake age 39 status 0
sadd employees 5
hmset employee:6 name mike age 20 status 1
sadd employees 6
And to get the data:
> sort employees get # get employee:*->name get employee:*->age get employee:*->stat
us
1) "1"
2) "clarke"
3) "24"
4) "1"
5) "2"
6) "danne"
7) "42"
8) "0"
9) "3"
10) "sachin"
11) "38"
12) "1"
13) "4"
14) "pasty"
15) "27"
16) "1"
17) "5"
18) "cake"
19) "39"
20) "0"
21) "6"
22) "mike"
23) "20"
24) "1"
Upvotes: 1
Reputation: 16043
The hashes which you have mentioned can be considered as different top-level keys in Redis. Hence, It's not possible to give a range. If you have the hash names with you, you'll have to get it one by one.
But, If you don't have the hash names, Redis supports regex in keys
command and you can make an appropriate regex to pass in this command to get your desired hash names, and then fetch one by one.
BUT, If you still want to do it in 1 call, Redis also provides support to run a Lua script and there you can fetch all the records in one go. Here's a link to the tutorial
Upvotes: 2