Skip to content
Snippets Groups Projects
Commit 84dcc50d authored by Robert Goldmann's avatar Robert Goldmann
Browse files

BaseUtils: improve cached services

parent 2287b234
No related branches found
No related tags found
No related merge requests found
...@@ -13,12 +13,12 @@ class CachedService(ABC): ...@@ -13,12 +13,12 @@ class CachedService(ABC):
def get_data(self) -> Dict: def get_data(self) -> Dict:
if self.__is_data_obsolete(): if self.__is_data_obsolete():
self._data = self._fetch_data() self._data = self._fetch_data()
self._lastFetchTimestamp = now = datetime.now().timestamp()
return self._data return self._data
def __is_data_obsolete(self) -> bool: def __is_data_obsolete(self) -> bool:
now = datetime.now().timestamp() now = datetime.now().timestamp()
if (now - self._lastFetchTimestamp) > self._fetchThresholdInSeconds: if (now - self._lastFetchTimestamp) > self._fetchThresholdInSeconds:
self._lastFetchTimestamp = now
return True return True
return False return False
......
...@@ -30,12 +30,12 @@ class MultiCacheKeyService(ABC): ...@@ -30,12 +30,12 @@ class MultiCacheKeyService(ABC):
cacheEntry = self._cache[cacheKey] cacheEntry = self._cache[cacheKey]
if self.__is_data_obsolete(cacheEntry): if self.__is_data_obsolete(cacheEntry):
cacheEntry.cachedData = self._fetch_data(cacheEntry.settings) cacheEntry.cachedData = self._fetch_data(cacheEntry.settings)
cacheEntry.lastFetchTimestamp = datetime.now().timestamp()
return cacheEntry.cachedData return cacheEntry.cachedData
def __is_data_obsolete(self, cacheEntry: CacheEntry) -> bool: def __is_data_obsolete(self, cacheEntry: CacheEntry) -> bool:
now = datetime.now().timestamp() now = datetime.now().timestamp()
if (now - cacheEntry.lastFetchTimestamp) > cacheEntry.fetchIntervalInSeconds: if (now - cacheEntry.lastFetchTimestamp) > cacheEntry.fetchIntervalInSeconds:
cacheEntry.lastFetchTimestamp = now
return True return True
return False return False
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment