Created
August 9, 2013 09:07
-
-
Save petrushev/6192270 to your computer and use it in GitHub Desktop.
example of storing nested data structures in redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
roomrate | |
======== | |
{ | |
'city': 'Berlin', | |
'hotel': 'Hilton', | |
'date': '2013-08-09' | |
'room_type': 'single', | |
'occupancy': 2, | |
'avail_count': 13, | |
'booked_count': 4, | |
'currency_iso_code': 'EUR', | |
'currency_name': 'Euro', | |
'provider': { | |
'name': 'SomeProvider', | |
'competitor_type': 'partner' | |
}, | |
'avail_room_nums': [3, 7, 23, 29, 41] | |
} | |
in redis: | |
# start transaction | |
MULTI | |
# set the attomic members of top level datastructure | |
# first part of the key is a namespace, the rest is the unique key | |
# the members that define the uniqueness go only in the key | |
# HMSET can be used to atommically(?) store multiple keys | |
HMSET roomrate:Berlin:Hilton:2013-08-09:single occupancy 2 avail_count 13 booked_count 4 currency_iso_code EUR currency_name Euro | |
# set a member that is a hash itself | |
HMSET roomrate:Berlin:Hilton:2013-08-09:single:provider name SomeProvider competitor_type partner | |
# set a member that is a set | |
SADD roomrate:Berlin:Hilton:2013-08-09:single:room_nums 3 7 23 29 41 | |
# commit | |
EXEC | |
# querying | |
HGETALL roomrate:Berlin:Hilton:2013-08-09:single | |
HGETALL roomrate:Berlin:Hilton:2013-08-09:single:provider | |
SMEMBERS roomrate:Berlin:Hilton:2013-08-09:single:room_nums | |
# each returns | |
1) "occupancy" | |
2) "2" | |
3) "avail_count" | |
4) "13" | |
5) "booked_count" | |
6) "4" | |
7) "currency_iso_code" | |
8) "EUR" | |
9) "currency_name" | |
10) "Euro" | |
1) "name" | |
2) "SomeProvider" | |
3) "competitor_type" | |
4) "partner" | |
1) "3" | |
2) "7" | |
3) "23" | |
4) "29" | |
5) "41" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment