file_cache.py 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2019/11/11 13:38:11
  7. @Version : 1.0
  8. @License : (C)Copyright 2019
  9. @Desc : 文件缓存
  10. '''
  11. import json
  12. import threading
  13. TOKEN="asfa"
  14. def persist_to_file(file_name):
  15. def decorator(original_func):
  16. try:
  17. cache = json.load(open(file_name, 'r'))
  18. except (IOError, ValueError):
  19. cache = {}
  20. def new_func(param):
  21. if param not in cache:
  22. cache[param] = original_func(param)
  23. json.dump(cache, open(file_name, 'w'), indent=4)
  24. return cache[param]
  25. return new_func
  26. return decorator
  27. @persist_to_file('cache.dat')
  28. def tenantid_to_tenant_name(tenantid):
  29. headers = {'X-Auth-Token': TOKEN}
  30. print(tenantid)
  31. return "I love you"
  32. if __name__ == "__main__":
  33. for i in range(100):
  34. tenantid_to_tenant_name(i)