Python Library Caching
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

property_cache_json.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class test_slow_data_cached(test_slow_data):
  45. def __init__(self, *args, **kwargs) -> None:
  46. super().__init__(*args, **kwargs)
  47. self._cache = caching.property_cache_json(test_slow_data(), 'cache.json')
  48. self._cache.add_source_get_keys(self.KEY_THREE)
  49. def get(self, key, default=None):
  50. return self._cache.get(key, default)
  51. if __name__ == "__main__":
  52. # Logging configuration
  53. logging.basicConfig(
  54. format="%(asctime)s: %(levelname)8s - %(name)s - %(message)s",
  55. level=logging.DEBUG,
  56. stream=sys.stdout
  57. )
  58. # Example
  59. tm = time.time()
  60. data = test_slow_data_cached()
  61. print('Testing property_cache (json):\n--------------------------------')
  62. for key in data.keys():
  63. print(data.get(key))
  64. print('--------------------------------\nThe execution time was %.1fs' % (time.time()-tm))