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

BaseUtils: v1.6.1; fetch interval adjustable per cache entry

parent 73362081
No related branches found
No related tags found
No related merge requests found
......@@ -8,19 +8,20 @@ from typing import Dict
@dataclass
class CacheEntry:
key: str
lastFetchTimestamp: float
fetchIntervalInSeconds: int
settings: Dict
lastFetchTimestamp: float
cachedData: Dict
class MultiCacheKeyService(ABC):
def __init__(self, fetchThresholdInSeconds: int):
self._fetchThresholdInSeconds = fetchThresholdInSeconds
def __init__(self):
self._cache = {}
def get_data(self, cacheKey: str, settings: Dict) -> Dict:
def get_data(self, cacheKey: str, fetchIntervalInSeconds: int, settings: Dict) -> Dict:
if cacheKey not in self._cache:
self._cache[cacheKey] = CacheEntry(cacheKey, datetime.now().timestamp(), settings, {})
self._cache[cacheKey] = CacheEntry(cacheKey, fetchIntervalInSeconds, settings,
datetime.now().timestamp(), {})
cacheEntry = self._cache[cacheKey]
if self.__is_data_obsolete(cacheEntry):
......@@ -29,7 +30,7 @@ class MultiCacheKeyService(ABC):
def __is_data_obsolete(self, cacheEntry: CacheEntry) -> bool:
now = datetime.now().timestamp()
if (now - cacheEntry.lastFetchTimestamp) > self._fetchThresholdInSeconds:
if (now - cacheEntry.lastFetchTimestamp) > cacheEntry.fetchIntervalInSeconds:
cacheEntry.lastFetchTimestamp = now
return True
......
......@@ -3,7 +3,7 @@ from setuptools import setup
setup(
name='TheCodeLabs-BaseUtils',
packages=['TheCodeLabs_BaseUtils'],
version='1.6.0',
version='1.6.1',
license='MIT',
description='Useful python classes',
author='TheCodeLabs',
......
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