Reputation: 254
I have several Mountain objects which have id and name.
I stored these objects like that
HMSET Mountain:1 id "1" name "Mo1"
HMSET Mountain:2 id "2" name "Mo2"
HMSET Mountain:3 id "3" name "Mo2"
How can I get all Mountain Objects? Or are there any better way storing objects in redis?
Upvotes: 0
Views: 1691
Reputation: 1192
You should do it with 2 command :
KEYS Mountain:*
HGETALL <everykeys>
But , if you can, it's better to not use KEYS command , so you can do something like this :
HMSET Mountain:1 id "1" name "Mo1"
SADD Montains Mountain:1
HMSET Mountain:2 id "2" name "Mo2"
SADD Montains Mountain:2
HMSET Mountain:3 id "3" name "Mo3"
SADD Montains Mountain:3
and get it :
SMEMBERS Mountain
HGETALL <everykeys>
Redis is a key/value system with extra data type, so you have to build your index So for having a name indexing for example, if name are unique:
HSET Mountains:IdByName "Mo3" 3
and you will get the id :
HGET Mountains:IdByName "Mo3"
for non unique lets use set again
SADD Mountains:IdByName:Mo3 3
And you will increase the number of keys and that why KEYS is not recommend , because to expensive
next step is use a lua script for having/setting hash from/and index
Upvotes: 3