Select Git revision
CachedService.py
-
Robert Goldmann authored
- data wouldn't be fetched again if get_data() is called repeatedly in less than fetchInterval
Robert Goldmann authored- data wouldn't be fetched again if get_data() is called repeatedly in less than fetchInterval
CachedService.py 758 B
import abc
from abc import ABC
from datetime import datetime
from typing import Dict
class CachedService(ABC):
def __init__(self, fetchThresholdInSeconds: int):
self._fetchThresholdInSeconds = fetchThresholdInSeconds
self._lastFetchTimestamp = 0
self._data = None
def get_data(self) -> Dict:
if self.__is_data_obsolete():
self._data = self._fetch_data()
return self._data
def __is_data_obsolete(self) -> bool:
now = datetime.now().timestamp()
if (now - self._lastFetchTimestamp) > self._fetchThresholdInSeconds:
self._lastFetchTimestamp = now
return True
return False
@abc.abstractmethod
def _fetch_data(self) -> Dict:
pass