2019-08-03 13:36:19 +08:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from collections import OrderedDict
|
2018-10-14 22:32:27 +08:00
|
|
|
from itertools import takewhile
|
|
|
|
import operator
|
2019-08-03 13:36:19 +08:00
|
|
|
import time
|
2018-10-14 22:32:27 +08:00
|
|
|
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
class IStorage(ABC):
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Local storage for this node.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
|
|
|
IStorage implementations of get must return the same type as put in
|
|
|
|
by set
|
2018-10-14 22:32:27 +08:00
|
|
|
"""
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
@abstractmethod
|
2018-10-14 22:32:27 +08:00
|
|
|
def __setitem__(self, key, value):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Set a key to the given value."""
|
2018-10-14 22:32:27 +08:00
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
@abstractmethod
|
2018-10-14 22:32:27 +08:00
|
|
|
def __getitem__(self, key):
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Get the given key.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
|
|
|
If item doesn't exist, raises C{KeyError}
|
2018-10-14 22:32:27 +08:00
|
|
|
"""
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
@abstractmethod
|
2018-10-14 22:32:27 +08:00
|
|
|
def get(self, key, default=None):
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Get given key.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
|
|
|
If not found, return default.
|
2018-10-14 22:32:27 +08:00
|
|
|
"""
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
@abstractmethod
|
|
|
|
def iter_older_than(self, seconds_old):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Return the an iterator over (key, value) tuples for items older than
|
|
|
|
the given seconds_old."""
|
2018-10-14 22:32:27 +08:00
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
@abstractmethod
|
2018-10-14 22:32:27 +08:00
|
|
|
def __iter__(self):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Get the iterator for this storage, should yield tuple of (key,
|
|
|
|
value)"""
|
2018-10-14 22:32:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ForgetfulStorage(IStorage):
|
|
|
|
def __init__(self, ttl=604800):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""By default, max age is a week."""
|
2018-10-14 22:32:27 +08:00
|
|
|
self.data = OrderedDict()
|
|
|
|
self.ttl = ttl
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key in self.data:
|
|
|
|
del self.data[key]
|
|
|
|
self.data[key] = (time.monotonic(), value)
|
|
|
|
self.cull()
|
|
|
|
|
|
|
|
def cull(self):
|
2019-01-16 01:41:41 +08:00
|
|
|
for _, _ in self.iter_older_than(self.ttl):
|
2018-10-14 22:32:27 +08:00
|
|
|
self.data.popitem(last=False)
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
self.cull()
|
|
|
|
if key in self.data:
|
|
|
|
return self[key]
|
|
|
|
return default
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
self.cull()
|
|
|
|
return self.data[key][1]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
self.cull()
|
|
|
|
return repr(self.data)
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def iter_older_than(self, seconds_old):
|
|
|
|
min_birthday = time.monotonic() - seconds_old
|
|
|
|
zipped = self._triple_iter()
|
|
|
|
matches = takewhile(lambda r: min_birthday >= r[1], zipped)
|
2018-10-14 22:32:27 +08:00
|
|
|
return list(map(operator.itemgetter(0, 2), matches))
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def _triple_iter(self):
|
2018-10-14 22:32:27 +08:00
|
|
|
ikeys = self.data.keys()
|
|
|
|
ibirthday = map(operator.itemgetter(0), self.data.values())
|
|
|
|
ivalues = map(operator.itemgetter(1), self.data.values())
|
|
|
|
return zip(ikeys, ibirthday, ivalues)
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def __iter__(self):
|
2018-10-14 22:32:27 +08:00
|
|
|
self.cull()
|
|
|
|
ikeys = self.data.keys()
|
|
|
|
ivalues = map(operator.itemgetter(1), self.data.values())
|
|
|
|
return zip(ikeys, ivalues)
|