forked from magnific0/nokia-weight-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessioncache.py
38 lines (30 loc) · 870 Bytes
/
sessioncache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# From https://github.com/cpfair/tapiriik
from datetime import datetime
class SessionCache:
def __init__(self, lifetime, freshen_on_get=False):
self._lifetime = lifetime
self._autorefresh = freshen_on_get
self._cache = {}
def Get(self, pk, freshen=False):
if pk not in self._cache:
return
record = self._cache[pk]
if record.Expired():
del self._cache[pk]
return None
if self._autorefresh or freshen:
record.Refresh()
return record.Get()
def Set(self, pk, value):
self._cache[pk] = SessionCacheRecord(value, self._lifetime)
class SessionCacheRecord:
def __init__(self, data, lifetime):
self._value = data
self._lifetime = lifetime
self.Refresh()
def Expired(self):
return self._timestamp < datetime.utcnow() - self._lifetime
def Refresh(self):
self._timestamp = datetime.utcnow()
def Get(self):
return self._value