Add maxage
This commit is contained in:
parent
988bfa5cc9
commit
97242dab2d
1 changed files with 27 additions and 12 deletions
|
@ -1,13 +1,19 @@
|
|||
import collections
|
||||
import itertools
|
||||
import time
|
||||
|
||||
class Cache:
|
||||
def __init__(self, maxlen=None):
|
||||
def __init__(self, maxage=None, maxlen=None, shrink_delay=None):
|
||||
self.maxage = maxage
|
||||
self.maxlen = maxlen
|
||||
self.shrink_delay = shrink_delay
|
||||
self.last_shrink = time.time()
|
||||
|
||||
self._dict = {}
|
||||
#self._order = collections.deque()
|
||||
self._recency = {}
|
||||
|
||||
def __getitem__(self, key):
|
||||
self._shrink()
|
||||
value = self._dict[key]
|
||||
self._maintain(key)
|
||||
return value
|
||||
|
@ -16,23 +22,32 @@ class Cache:
|
|||
self._shrink()
|
||||
self._dict[key] = value
|
||||
self._maintain(key)
|
||||
#print(self._dict, self._recency)
|
||||
|
||||
def _maintain(self, key):
|
||||
self._recency[key] = time.time()
|
||||
|
||||
def _shrink(self):
|
||||
if self.shrink_delay is not None:
|
||||
if time.time() - self.last_shrink > self.shrink_delay:
|
||||
return
|
||||
|
||||
now = time.time()
|
||||
self.last_shrink = now
|
||||
|
||||
if self.maxlen is None:
|
||||
return
|
||||
|
||||
pop_count = len(self._dict) - self.maxlen
|
||||
if pop_count < 1:
|
||||
return
|
||||
|
||||
pop_count = 0
|
||||
else:
|
||||
pop_count = len(self._dict) - self.maxlen
|
||||
keys = sorted(self._recency.keys(), key=self._recency.get)
|
||||
for key in keys[:pop_count]:
|
||||
self._dict.pop(key)
|
||||
self._recency.pop(key)
|
||||
for key in itertools.islice(keys, 0, pop_count):
|
||||
self.remove(key)
|
||||
|
||||
if self.maxage is not None:
|
||||
for key in itertools.islice(keys, pop_count, None):
|
||||
last_used = self._recency[key]
|
||||
age = now - last_used
|
||||
if age > self.maxage:
|
||||
self.remove(key)
|
||||
|
||||
def get(self, key, fallback=None):
|
||||
try:
|
||||
|
|
Loading…
Reference in a new issue