Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/session/SessionCache.py
5925 views
1
from datetime import datetime, timedelta
2
3
4
class SessionCache:
5
""" Class Cache session data
6
"""
7
STOCK_AVAILABLE = 'STOCK_AVAILABLE'
8
9
# ToDo ReImplant with using Redis
10
_SESSION_CACHE = None
11
12
@classmethod
13
def get_session_cache(cls):
14
if cls._SESSION_CACHE is None:
15
cls._SESSION_CACHE = SessionCache()
16
cls._SESSION_CACHE._cache_live_hours = 1 # ToDo Read from config
17
return cls._SESSION_CACHE
18
19
def __init__(self):
20
self._cache_data = dict()
21
self._cache_live_hours = 1
22
23
def cache_data(self, group, key, data):
24
if group not in self._cache_data:
25
self._cache_data[group] = dict()
26
self._cache_data[group][key] = dict(data=data, cached_datetime=datetime.today().now())
27
28
def get_cached(self, group_key, key):
29
if group_key in self._cache_data:
30
group = self._cache_data[group_key]
31
if key in group:
32
cached_data = group[key]
33
cached_datetime = cached_data['cached_datetime']
34
live_time = cached_datetime - timedelta(hours=self._cache_live_hours)
35
if cached_datetime <= live_time:
36
return cached_data['data']
37
return None
38
39