Python Library Caching
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

property_cache_json.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import caching
  4. import logging
  5. import sys
  6. import time
  7. class test_slow_data(object):
  8. DATA_VERSION = 0.1
  9. KEY_ONE = '1'
  10. KEY_TWO = '2'
  11. KEY_THREE = 'three'
  12. KEY_FOUR = 'four'
  13. KEY_FIVE = 'five'
  14. KEYS = [KEY_ONE, KEY_TWO, KEY_THREE, KEY_FOUR, KEY_FIVE]
  15. def data_version(self):
  16. return self.DATA_VERSION
  17. def get(self, key, default=None):
  18. try:
  19. return getattr(self, f'__{key}__')()
  20. except AttributeError:
  21. return default
  22. def keys(self):
  23. return self.KEYS
  24. def uid(self):
  25. return None
  26. def print_n_sleep(self, k):
  27. sys.stdout.write('slow get executed for %s\n' % k)
  28. time.sleep(3)
  29. def __1__(self):
  30. self.print_n_sleep("__1__")
  31. return 'one'
  32. def __2__(self):
  33. self.print_n_sleep("__2__")
  34. return 'two'
  35. def __three__(self):
  36. self.print_n_sleep("__three__")
  37. return 'three'
  38. def __four__(self):
  39. self.print_n_sleep("__four__")
  40. return 'four'
  41. def __five__(self):
  42. self.print_n_sleep("__five__")
  43. return 'five'
  44. if __name__ == "__main__":
  45. # Logging configuration
  46. logging.basicConfig(
  47. format="%(asctime)s: %(levelname)8s - %(name)s - %(message)s",
  48. level=logging.DEBUG,
  49. stream=sys.stdout
  50. )
  51. # Example
  52. tm = time.time()
  53. data = caching.property_cache_json(test_slow_data(), 'cache.json')
  54. data.add_source_get_keys(data.KEY_THREE)
  55. print('Testing property_cache (json):\n--------------------------------')
  56. for key in data.keys():
  57. print(data.get(key))
  58. print('--------------------------------\nThe execution time was %.1fs' % (time.time()-tm))