如何使用 Python-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'}],
}
想要添加到 session 列表以及添加新 session 并检索它们。我如何使用 Python-redis 执行此操作?
最佳答案
用 pickle 序列化你的字典 {'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}
或 json 。使用 redis 列表将它们存储为字符串。使用 browsing_history:SESSION_KEY_1
之类的键访问这些列表。如果您需要获取所有 session key 的列表,您可能需要维护一组 key 字符串 browsing_history:*
。
关于python - Redis 中的字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664664/