作者:E4b9a6, 创建:2021-06-15, 字数:793, 已阅:58, 最后更新:2021-06-15
缓存字典到本地(支持多线程写入/读取)
def GetIDTagDictByLocalTMP(id_tag: str):
if ID_TAG_CACHE.get(id_tag):
return ID_TAG_CACHE.get(id_tag)
id_tag_dict = {'id': id_tag}
SetIDTagDictToLocalTMP(id_tag, id_tag_dict)
return id_tag_dict
def SetIDTagDictToLocalTMP(id_tag: str, id_tag_dict: dict):
if not os.path.exists('tmp'):
os.mkdir('tmp')
ID_TAG_CACHE[id_tag] = id_tag_dict
with open('cache/ID-TAG.json', 'w') as f:
f.write(json.dumps(ID_TAG_CACHE, ensure_ascii=False))
def BuildIDTagCacheByLocalTmp():
global ID_TAG_CACHE
if not os.path.exists('cache'):
os.mkdir('cache')
if not os.path.exists('cache/ID-TAG.json'):
return
with open('cache/ID-TAG.json', 'r') as f:
ID_TAG_CACHE = json.loads(f.read())