#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Author : liuyuqi @Contact : liuyuqi.gov@msn.cn @Time : 2019/11/11 13:38:11 @Version : 1.0 @License : (C)Copyright 2019 @Desc : 文件缓存 ''' import json import threading TOKEN="asfa" def persist_to_file(file_name): def decorator(original_func): try: cache = json.load(open(file_name, 'r')) except (IOError, ValueError): cache = {} def new_func(param): if param not in cache: cache[param] = original_func(param) json.dump(cache, open(file_name, 'w'), indent=4) return cache[param] return new_func return decorator @persist_to_file('cache.dat') def tenantid_to_tenant_name(tenantid): headers = {'X-Auth-Token': TOKEN} print(tenantid) return "I love you" if __name__ == "__main__": for i in range(100): tenantid_to_tenant_name(i)