menu Chancel's blog
rss_feed
Chancel's blog
时来天地皆同力

Python3本地字典缓存

作者:Chancel Yang, 时间:2021 Jun 15, 阅读:35

缓存字典到本地(支持多线程写入/读取)

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())

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]