store on get implemented

This commit is contained in:
Dirk Alders 2024-09-15 15:12:02 +02:00
parent c57a885193
commit 06cf512f8f

View File

@ -87,13 +87,13 @@ 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_none_value=False): def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True):
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_none_value = store_none_value self._store_on_get = store_on_get
self._cached_props = None self._cached_props = None
def get(self, key, default=None): def get(self, key, default=None):
@ -116,7 +116,7 @@ class property_cache_pickle(object):
if self._key_filter(key) not in self._cached_props[self.DATA_TAG] or cache_old: if self._key_filter(key) not in self._cached_props[self.DATA_TAG] or cache_old:
val = self._source_instance.get(key, None) val = self._source_instance.get(key, None)
logger.debug("%s Loading property for '%s' from source instance (%s)", self.LOG_PREFIX, key, repr(val)) logger.debug("%s Loading property for '%s' from source instance (%s)", self.LOG_PREFIX, key, repr(val))
if val or self._store_none_value: if self._store_on_get:
tm = int(time.time()) tm = int(time.time())
logger.debug("Storing value=%s with timestamp=%d to chache", val, tm) logger.debug("Storing value=%s with timestamp=%d to chache", val, tm)
self._cached_props[self.DATA_TAG][self._key_filter(key)] = val self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
@ -132,6 +132,10 @@ class property_cache_pickle(object):
logger.info("%s Key '%s' is not in cached_keys. Uncached data will be returned.", self.LOG_PREFIX, key) logger.info("%s Key '%s' is not in cached_keys. Uncached data will be returned.", self.LOG_PREFIX, key)
return self._source_instance.get(key, default) return self._source_instance.get(key, default)
def full_update(self):
self._load_source()
self._save_cache()
def keys(self): def keys(self):
""" """
Method to get the available keys (from :data:`source_instance`). Method to get the available keys (from :data:`source_instance`).
@ -163,6 +167,7 @@ class property_cache_pickle(object):
storage_version = True storage_version = True
# #
if not load_cache or uid or data_version or storage_version: if not load_cache or uid or data_version or storage_version:
if load_cache:
if self._uid() is not None and uid: if self._uid() is not None and uid:
logger.debug("%s Source uid changed, ignoring previous cache data", self.LOG_PREFIX) logger.debug("%s Source uid changed, ignoring previous cache data", self.LOG_PREFIX)
if self._data_version() is not None and data_version: if self._data_version() is not None and data_version:
@ -191,10 +196,11 @@ class property_cache_pickle(object):
return key return key
def _load_source(self): def _load_source(self):
if self._cached_props is None:
self._init_cache()
logger.debug('%s Loading all data from source - %s', self.LOG_PREFIX, repr(self._source_instance.keys())) logger.debug('%s Loading all data from source - %s', self.LOG_PREFIX, repr(self._source_instance.keys()))
for key in self._source_instance.keys(): for key in self._source_instance.keys():
val = self._source_instance.get(key) self._cached_props[self.DATA_TAG][self._key_filter(key)] = self._source_instance.get(key)
self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
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())
def _save_cache(self): def _save_cache(self):