Python Library Caching
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

property_cache_pickle.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. import time
  5. sys.path.append('../..')
  6. import caching
  7. import report
  8. report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])
  9. class test_slow_data(object):
  10. _ONE = '1'
  11. _TWO = '2'
  12. _THREE = '_property_cache_data_version_'
  13. _FOUR = '_property_cache_uid_'
  14. _FIVE = '__property_cache_uid_'
  15. KEYS = [_ONE, _TWO, _THREE, _FOUR, _FIVE]
  16. VERS = 0.1
  17. def data_version(self):
  18. return self.VERS
  19. def one(self):
  20. return self.get(self._ONE)
  21. def two(self):
  22. return self.get(self._TWO)
  23. def three(self):
  24. return self.get(self._THREE)
  25. def four(self):
  26. return self.get(self._FOUR)
  27. def five(self):
  28. return self.get(self._FIVE)
  29. def get(self, key, default=None):
  30. def print_n_sleep(k):
  31. sys.stdout.write('slow get executed for %s\n' % k)
  32. time.sleep(3)
  33. if key == self._ONE:
  34. print_n_sleep(key)
  35. return 'one'
  36. if key == self._TWO:
  37. print_n_sleep(key)
  38. return 'two'
  39. if key == self._THREE:
  40. print_n_sleep(key)
  41. return 'three'
  42. if key == self._FOUR:
  43. print_n_sleep(key)
  44. return 'four'
  45. if key == self._FIVE:
  46. print_n_sleep(key)
  47. return 'five'
  48. return default
  49. def keys(self):
  50. return self.KEYS
  51. def uid(self):
  52. return None
  53. class tsd_cache_pickle(test_slow_data):
  54. def __init__(self, *args, **kwargs):
  55. test_slow_data.__init__(self, *args, **kwargs)
  56. self._cached_data = caching.property_cache_pickle(test_slow_data(*args, **kwargs), 'cache.pickle', load_all_on_init=False)
  57. def two(self):
  58. return test_slow_data.get(self, self._TWO)
  59. def get(self, key, default=None):
  60. return self._cached_data.get(key, default)
  61. data = tsd_cache_pickle()
  62. print('Testing property_cache (pickle):\\n--------------------------------')
  63. print(data.one())
  64. print(data.two())
  65. print(data.three())
  66. print(data.four())
  67. print(data.five())