add state.cache and cache some stuff
This commit is contained in:
parent
5b33721c87
commit
56be46d82e
4 changed files with 56 additions and 24 deletions
20
oml/cache.py
Normal file
20
oml/cache.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import time
|
||||
|
||||
class Cache(dict):
|
||||
|
||||
def __init__(self, ttl=10):
|
||||
self._ttl = ttl
|
||||
self._added = {}
|
||||
|
||||
def get(self, key):
|
||||
if key in self._added:
|
||||
if self._added[key] < time.time():
|
||||
del self._added[key]
|
||||
del self[key]
|
||||
return
|
||||
return dict.__getitem__(self, key)
|
||||
|
||||
def set(self, key, value, ttl=None):
|
||||
ttl = ttl or self._ttl
|
||||
self._added[key] = time.time() + ttl
|
||||
dict.__setitem__(self, key, value)
|
||||
Loading…
Add table
Add a link
Reference in a new issue