Release 577b0566ea65d16ab78f897274c3f04f
This commit is contained in:
parent
104e25e74f
commit
ed1082041f
36
__init__.py
36
__init__.py
@ -97,13 +97,14 @@ class property_cache_pickle(object):
|
|||||||
#
|
#
|
||||||
STORAGE_VERSION = 1
|
STORAGE_VERSION = 1
|
||||||
|
|
||||||
def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True):
|
def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True, return_source_on_none=False):
|
||||||
self._source_instance = source_instance
|
self._source_instance = source_instance
|
||||||
self._cache_filename = cache_filename
|
self._cache_filename = cache_filename
|
||||||
self._load_all_on_init = load_all_on_init
|
self._load_all_on_init = load_all_on_init
|
||||||
self._callback_on_data_storage = callback_on_data_storage
|
self._callback_on_data_storage = callback_on_data_storage
|
||||||
self._max_age = max_age
|
self._max_age = max_age
|
||||||
self._store_on_get = store_on_get
|
self._store_on_get = store_on_get
|
||||||
|
self._return_source_on_none = return_source_on_none
|
||||||
#
|
#
|
||||||
self._source_get_keys = []
|
self._source_get_keys = []
|
||||||
self._cached_props = None
|
self._cached_props = None
|
||||||
@ -163,11 +164,14 @@ class property_cache_pickle(object):
|
|||||||
return val
|
return val
|
||||||
else:
|
else:
|
||||||
logger.debug("Providing property for '%s' from cache", key)
|
logger.debug("Providing property for '%s' from cache", key)
|
||||||
return self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
|
cached_data = self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
|
||||||
|
if cached_data is None and self._return_source_on_none:
|
||||||
|
return self._source_instance.get(key, default)
|
||||||
|
return cached_data
|
||||||
else:
|
else:
|
||||||
if key not in self._source_instance.keys():
|
if key not in self._source_instance.keys():
|
||||||
logger.debug("Key '%s' is not in cached_keys. Uncached data will be returned.", key)
|
logger.debug("Key '%s' is not in cached_keys. Uncached data will be returned.", key)
|
||||||
elif key in self._source_get_keys:
|
else:
|
||||||
logger.debug("Key '%s' is excluded by .add_source_get_keys(). Uncached data will be returned.", key)
|
logger.debug("Key '%s' is excluded by .add_source_get_keys(). Uncached data will be returned.", key)
|
||||||
return self._source_instance.get(key, default)
|
return self._source_instance.get(key, default)
|
||||||
|
|
||||||
@ -209,14 +213,15 @@ class property_cache_pickle(object):
|
|||||||
self._cached_props[self.UID_TAG] = self._source_instance.uid()
|
self._cached_props[self.UID_TAG] = self._source_instance.uid()
|
||||||
self._cached_props[self.DATA_VERSION_TAG] = self._source_instance.data_version()
|
self._cached_props[self.DATA_VERSION_TAG] = self._source_instance.data_version()
|
||||||
self._cached_props[self.STORAGE_VERSION_TAG] = self.STORAGE_VERSION
|
self._cached_props[self.STORAGE_VERSION_TAG] = self.STORAGE_VERSION
|
||||||
if self._store_on_get:
|
|
||||||
self._save_cache()
|
|
||||||
|
|
||||||
def _load_cache(self):
|
def _load_only(self):
|
||||||
if os.path.exists(self._cache_filename):
|
|
||||||
with open(self._cache_filename, 'rb') as fh:
|
with open(self._cache_filename, 'rb') as fh:
|
||||||
self._cached_props = pickle.load(fh)
|
self._cached_props = pickle.load(fh)
|
||||||
logger.debug('Loading properties from cache (%s)', self._cache_filename)
|
logger.debug('Loading properties from cache (%s)', self._cache_filename)
|
||||||
|
|
||||||
|
def _load_cache(self):
|
||||||
|
if os.path.exists(self._cache_filename):
|
||||||
|
self._load_only()
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logger.debug('Cache file does not exists (yet).')
|
logger.debug('Cache file does not exists (yet).')
|
||||||
@ -235,10 +240,13 @@ class property_cache_pickle(object):
|
|||||||
self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
|
self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
|
||||||
time.sleep(sleep_between_keys)
|
time.sleep(sleep_between_keys)
|
||||||
|
|
||||||
def _save_cache(self):
|
def _save_only(self):
|
||||||
with open(self._cache_filename, 'wb') as fh:
|
with open(self._cache_filename, 'wb') as fh:
|
||||||
pickle.dump(self._cached_props, fh)
|
pickle.dump(self._cached_props, fh)
|
||||||
logger.debug('cache-file stored (%s)', self._cache_filename)
|
logger.debug('cache-file stored (%s)', self._cache_filename)
|
||||||
|
|
||||||
|
def _save_cache(self):
|
||||||
|
self._save_only()
|
||||||
if self._callback_on_data_storage is not None:
|
if self._callback_on_data_storage is not None:
|
||||||
self._callback_on_data_storage(self)
|
self._callback_on_data_storage(self)
|
||||||
|
|
||||||
@ -271,19 +279,13 @@ class property_cache_json(property_cache_pickle):
|
|||||||
|
|
||||||
.. literalinclude:: caching/_examples_/property_cache_json_2.log
|
.. literalinclude:: caching/_examples_/property_cache_json_2.log
|
||||||
"""
|
"""
|
||||||
def _load_cache(self):
|
|
||||||
if os.path.exists(self._cache_filename):
|
def _load_only(self):
|
||||||
with open(self._cache_filename, 'r') as fh:
|
with open(self._cache_filename, 'r') as fh:
|
||||||
self._cached_props = json.load(fh)
|
self._cached_props = json.load(fh)
|
||||||
logger.debug('Loading properties from cache (%s)', self._cache_filename)
|
logger.debug('Loading properties from cache (%s)', self._cache_filename)
|
||||||
return True
|
|
||||||
else:
|
|
||||||
logger.debug('Cache file does not exists (yet).')
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _save_cache(self):
|
def _save_only(self):
|
||||||
with open(self._cache_filename, 'w') as fh:
|
with open(self._cache_filename, 'w') as fh:
|
||||||
json.dump(self._cached_props, fh, sort_keys=True, indent=4)
|
json.dump(self._cached_props, fh, sort_keys=True, indent=4)
|
||||||
logger.debug('cache-file stored (%s)', self._cache_filename)
|
logger.debug('cache-file stored (%s)', self._cache_filename)
|
||||||
if self._callback_on_data_storage is not None:
|
|
||||||
self._callback_on_data_storage(self)
|
|
||||||
|
BIN
_requirements_/specification.pdf
Normal file
BIN
_requirements_/specification.pdf
Normal file
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user