TAGS :Viewed: 14 - Published at: a few seconds ago

[ Save redis hash instead of string ]

I can save a dict as a string in redis as follows:

>>> r.set( 
           'rt.http://rottentomatoes.com/m/771354525/', 
           {"Director": "blasco ricardo", "Platform": "RT"}
         )

How would I save the dict as a dict/hash directly so I wouldn't have to use json.loads to read it into a dict? Currently, if I do r.get() I get the dict as a string:

>>> r.get('rt.http://rottentomatoes.com/m/771354525/')
'{"Director": "blasco ricardo", "Platform": "RT"}'

Answer 1


Look into hmset

HMSET 'rt.http://rottentomatoes.com/m/771354525/' Director "blasco ricardo" Platform "RT"

Then you can retrieve it with

HGETALL rt.http://rottentomatoes.com/m/771354525/

Or a specific field with

HGET rt.http://rottentomatoes.com/m/771354525/ Director

In python it would be

r.hmset('rt.http://rottentomatoes.com/m/771354525/', {'Director': 'blasco ricardo', 'Platform': 'RT'})
r.hgetall('rt.http://rottentomatoes.com/m/771354525/')
r.hget('rt.http://rottentomatoes.com/m/771354525/', 'Director')