目前,我正在用str()编写我的字典,然后将它存储在redis中。当我想修改对象时,我从redis获取它并使用eval()。我发现也可以使用泡菜模块来做同样的事情。哪个更有效率,哪个更好?
obj = # very large and deeply nested dictionary
cache = redis.StrictRedis(host='localhost', port=6379, db=0)
cache.set('id', str(obj))
cache.get('id')或
obj = # very large and deeply nested dictionary
cache = redis.StrictRedis(host='localhost', port=6379, db=0)
cache.set('id', pickle.dumps(obj))
pickle.loads(cache.get('id'))发布于 2018-02-25 20:50:09
因为您使用的是嵌套字典,所以redis只支持
Redis散列是字符串字段和字符串值之间的映射。
那么使用json模块的最简单方法
import json
your_dict = {}
json.dumps(your_dict)
# and to load it
your_dict_in_str = '{}'
json.loads(your_dict_in_str)并尽量避免使用eval
https://stackoverflow.com/questions/48978121
复制相似问题