Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store json data in redis #798

Closed
abhinav76 opened this issue Aug 25, 2015 · 9 comments
Closed

store json data in redis #798

abhinav76 opened this issue Aug 25, 2015 · 9 comments
Labels

Comments

@abhinav76
Copy link

I am very new to redis. So what i want is store the data retrieved from database. Now do i have to use client.set() for every key-value pair returned by database or is there any other way i can save all the data with one command? Data is of the form:

{
"member_username": "nikhil567",
"member_gender": "Male",
"member_dob": 0,
"member_country": "",
}

@stockholmux
Copy link
Contributor

You probably want to use a hash in Redis. With a hash you can use client.hmset (more info) to store your object.

One note, JS Objects can be nested, but Redis Hashes are only a single level. So, if your data looks something like this:

{ member_name : 'johndoe', address : { street: '123 Any Street', city: 'Anytown' } }

You'll need to use flat before storing it in redis. I wrote an article about that process.

@abhinav76
Copy link
Author

@stockholmux my data is stored in variable member. so how would i go about it?
var member={member_name:'svf',address:'cvbjnk'}
can you please give me the example query?

@kulicuu
Copy link

kulicuu commented Aug 25, 2015

The proper way is hashed as explained above, and flat, as also explained. Although that librarymodule is new to me.
There is a shortcut, which is generally to be frowned upon (tldr will make it slower to crawl over data analytically later), but maybe sometimes appropriate: JSON.stringify your object and store it in a normal key as a string.

member_string = JSON.stringify member
redis_client.set 'your_key', member_string
# something like that
# if your object is already flat then you can (just):
redis_client.hmset 'hash_key', member
# something like that, been awhile

@stockholmux
Copy link
Contributor

@abhinav76 It really depends on how you are accessing the data, but you'll likely want to access by your member_name, right? So, pull that into the key.

var member={member_name:'svf',address:'cvbjnk'};

client.hmset('members:'+member.member_name, member, function(err) { ... });

Later, if you want to get that hash and all the fields you'd do:

client.hgetall('members:svf', function(err,values) { ... });

You may also want to look into some sort of secondary store to keep track of all your member:* hashes - something like a redis set or list

@abhinav76
Copy link
Author

thanks @kulicuu and @stockholmux .

@abhinav76
Copy link
Author

My redis data looks like below:
now how can i update any of the fields in the this data?
@kulicuu @stockholmux

"{\"f_name\":\"\",\"profile\":{\"member_username\":\"nikhil371\",\"member_gender\":\"Male\",\"member_dob\":0,\"member_country\":\"\",\"state\":\"\",\"city\":\"\",\"hair_colour\":\"\",\"hairstyle\":\"\",\"eye_colour\":\"\",\"eyesight\":\"\",\"complexion\":\"\",\"height\":\"\",\"body_ENGINE\":\"\",\"exercise\":\"\",\"religion\":\"\",\"religious_beliefs\":\"\",\"education\":\"\",\"occupation\":\"\",\"annual_income\":\"\",\"ethnicity\":\"\",\"nationality\":\"\",\"languages\":\"\",\"diet\":\"\",\"smoke\":\"\",\"drink\":\"\",\"current_relationship_status\":\"\",\"preferred_relationship\":\"\",\"profile_tagline\":\"\",\"screened\":\"N\",\"photo_status\":\"None\",\"hidden\":\"N\",\"record_date\":0,\"update_date\":0,\"ip\":\"\",\"member_id\":10548,\"time_stamp\":0,\"photo_key\":\"\",\"profile_personality\":{\"member_username\":\"nikhil371\",\"feature\":\"xcvbnkm\",\"zodiac\":\"vbnim\",\"favorites\":\"qerrb\",\"motto\":\"cvbn\",\"irritated\":\"aguweihjok\",\"sexiest\":\"atwvyebuinmo\",\"good_evening\":\"ywsbciobnom\",\"like_to_do\":\"vdsfd371\",\"outgoing\":\"ev\",\"prefer_to\":\"ebvjnk\",\"record_date\":371,\"update_date\":372,\"profile_other_info\":{\"id\":10516,\"member_username\":\"nikhil371\",\"describe_yourself\":\"tyuh\",\"views\":371,\"admin_login\":\"\",\"screening_assigned_to\":\"\"}}}}"

@stockholmux
Copy link
Contributor

@kulicuu If you used JSON.stringify, you'll need to use JSON.parse to de-serialize the data, update your field, then you'll need to re-write it to the same key with JSON.stringify. It's pretty wasteful both in terms of CPU and space-efficiency.

Using hashes, you can update a single field by doing something like this client.hset('my:key:name','member_gender','female'). If you used flat to un-nest your object, you could update a single filed with client.hset('my:key:name','profile_personality.member_username','asdf').

@kulicuu
Copy link

kulicuu commented Aug 26, 2015

I agree it's to be frowned upon-- and thanks @stockholmux for elaborating on the discussion of why that is--, but it's worth mentioning it as a shortcut.

For context, here's something I wrote about 8 months ago. I have had to defer the major refactor which would have moved most of the models into Redis' Lua scripts.
https://github.com/Terebinth/britvic/blob/deprecated_version/server/models/user_iced.iced
(the variable d is the redis client)
Check out some of the other models too. @abhinav76

@abhinav76
Copy link
Author

@stockholmux i tried to use flat and it gives me error: MAximum call stack size exceeded ..
do you have any idea about that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants