List of Dicts in Redis

Serialize your dictionary {'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'} with pickle or json. Use redis list to store them as strings. Use keys like browsing_history:SESSION_KEY_1 to access those lists. If you need to get a list of all session keys you probably will need to maintain a set of strings for keys browsing_history:*.


A solution not requiring serialization and not restricted by the limits on string sizes (but not necessarily more performant) is to store each dict in its own dedicated hash map:

# define root name for hashes used 
# to store list elements - dicts
hash_root_name='test_hash'

# sample list of dicts
dicts_list=[test_dict1, test_dict2]

# store dicts from the list to consecutively 
# named redis hashes,  with list indices 
# appended to hash root name
for i in range(len(dicts_list)):
    
    redis_client.hmset(hash_root_name+str(i), 
                       dicts_list[i]) 

Tags:

Python

Redis