Skip to content
Snippets Groups Projects
Select Git revision
  • 29c46f0e7fabe8417fcb69bb9d055a09b5b2eea0
  • master default
2 results

CachedService.py

Blame
  • 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