Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/session/SessionCache.py
5925 views
from datetime import datetime, timedelta123class SessionCache:4""" Class Cache session data5"""6STOCK_AVAILABLE = 'STOCK_AVAILABLE'78# ToDo ReImplant with using Redis9_SESSION_CACHE = None1011@classmethod12def get_session_cache(cls):13if cls._SESSION_CACHE is None:14cls._SESSION_CACHE = SessionCache()15cls._SESSION_CACHE._cache_live_hours = 1 # ToDo Read from config16return cls._SESSION_CACHE1718def __init__(self):19self._cache_data = dict()20self._cache_live_hours = 12122def cache_data(self, group, key, data):23if group not in self._cache_data:24self._cache_data[group] = dict()25self._cache_data[group][key] = dict(data=data, cached_datetime=datetime.today().now())2627def get_cached(self, group_key, key):28if group_key in self._cache_data:29group = self._cache_data[group_key]30if key in group:31cached_data = group[key]32cached_datetime = cached_data['cached_datetime']33live_time = cached_datetime - timedelta(hours=self._cache_live_hours)34if cached_datetime <= live_time:35return cached_data['data']36return None373839