|
@ -13,52 +13,52 @@ class LRUCache(collections.MutableMapping): |
|
|
def __init__(self, timeout=60, close_callback=None, *args, **kwargs): |
|
|
def __init__(self, timeout=60, close_callback=None, *args, **kwargs): |
|
|
self.timeout = timeout |
|
|
self.timeout = timeout |
|
|
self.close_callback = close_callback |
|
|
self.close_callback = close_callback |
|
|
self.store = {} |
|
|
self._store = {} |
|
|
self.time_to_keys = collections.defaultdict(list) |
|
|
self._time_to_keys = collections.defaultdict(list) |
|
|
self.last_visits = [] |
|
|
self._last_visits = [] |
|
|
self.update(dict(*args, **kwargs)) # use the free update to set keys |
|
|
self.update(dict(*args, **kwargs)) # use the free update to set keys |
|
|
|
|
|
|
|
|
def __getitem__(self, key): |
|
|
def __getitem__(self, key): |
|
|
"O(logm)" |
|
|
# O(logm) |
|
|
t = time.time() |
|
|
t = time.time() |
|
|
self.time_to_keys[t].append(key) |
|
|
self._time_to_keys[t].append(key) |
|
|
heapq.heappush(self.last_visits, t) |
|
|
heapq.heappush(self._last_visits, t) |
|
|
return self.store[key] |
|
|
return self._store[key] |
|
|
|
|
|
|
|
|
def __setitem__(self, key, value): |
|
|
def __setitem__(self, key, value): |
|
|
"O(logm)" |
|
|
# O(logm) |
|
|
t = time.time() |
|
|
t = time.time() |
|
|
self.store[key] = value |
|
|
self._store[key] = value |
|
|
self.time_to_keys[t].append(key) |
|
|
self._time_to_keys[t].append(key) |
|
|
heapq.heappush(self.last_visits, t) |
|
|
heapq.heappush(self._last_visits, t) |
|
|
|
|
|
|
|
|
def __delitem__(self, key): |
|
|
def __delitem__(self, key): |
|
|
"O(1)" |
|
|
# O(1) |
|
|
del self.store[key] |
|
|
del self._store[key] |
|
|
|
|
|
|
|
|
def __iter__(self): |
|
|
def __iter__(self): |
|
|
return iter(self.store) |
|
|
return iter(self._store) |
|
|
|
|
|
|
|
|
def __len__(self): |
|
|
def __len__(self): |
|
|
return len(self.store) |
|
|
return len(self._store) |
|
|
|
|
|
|
|
|
def sweep(self): |
|
|
def sweep(self): |
|
|
"O(m)" |
|
|
# O(m) |
|
|
now = time.time() |
|
|
now = time.time() |
|
|
c = 0 |
|
|
c = 0 |
|
|
while len(self.last_visits) > 0: |
|
|
while len(self._last_visits) > 0: |
|
|
least = self.last_visits[0] |
|
|
least = self._last_visits[0] |
|
|
if now - least <= self.timeout: |
|
|
if now - least <= self.timeout: |
|
|
break |
|
|
break |
|
|
for key in self.time_to_keys[least]: |
|
|
for key in self._time_to_keys[least]: |
|
|
heapq.heappop(self.last_visits) |
|
|
heapq.heappop(self._last_visits) |
|
|
if self.store.__contains__(key): |
|
|
if self._store.__contains__(key): |
|
|
value = self.store[key] |
|
|
value = self._store[key] |
|
|
if self.close_callback is not None: |
|
|
if self.close_callback is not None: |
|
|
self.close_callback(value) |
|
|
self.close_callback(value) |
|
|
|
|
|
|
|
|
del self.store[key] |
|
|
del self._store[key] |
|
|
c += 1 |
|
|
c += 1 |
|
|
del self.time_to_keys[least] |
|
|
del self._time_to_keys[least] |
|
|
if c: |
|
|
if c: |
|
|
logging.debug('%d keys swept' % c) |
|
|
logging.debug('%d keys swept' % c) |
|
|