2011-12-29 75 views
4

如何使用Python-redis在Redis中保留一個字典列表。下面是我的目標在數據結構:Redis中的字典列表

'browsing_history' : { 
    'session_key_1' : [{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}, {''image': 'image-url2', 'url' : 'url2', 'title' : 'test_title2', 'description' : 'test_description2'}], 
    'session_key_2' : [{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}, {''image': 'image-url2', 'url' : 'url2', 'title' : 'test_title2', 'description' : 'test_description2'}], 
} 

想添加到會話列表以及添加新的會話,也retrive他們。我如何使用Python-redis來做到這一點?

回答

7

序列化您的字典{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}picklejson。使用redis列表將它們存儲爲字符串。使用像browsing_history:SESSION_KEY_1這樣的鍵來訪問這些列表。如果您需要獲取所有會話密鑰的列表,您可能需要爲密鑰browsing_history:*維護一組字符串。

+0

這在面值方面很有意思,但我很關心閱讀和迭代。遍歷這個列表需要對每個項目進行反序列化。在這種方法下追加速度很快。 – Tommy 2018-02-01 19:16:28

+0

當從任何來源加載數據到python對象時,反序列化步驟一直是強制性的。大多數數據庫驅動程序爲您這樣做往往是不可見的。 – Ski 2018-02-02 03:57:34