From 16db66675bb1160b4104140a7927036c5552333c Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 19 May 2015 08:35:28 +0800 Subject: [PATCH] optimize LRUCache --- shadowsocks/lru_cache.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shadowsocks/lru_cache.py b/shadowsocks/lru_cache.py index b70f743..401f19b 100644 --- a/shadowsocks/lru_cache.py +++ b/shadowsocks/lru_cache.py @@ -41,6 +41,7 @@ class LRUCache(collections.MutableMapping): self._time_to_keys = collections.defaultdict(list) self._keys_to_last_time = {} self._last_visits = collections.deque() + self._closed_values = set() self.update(dict(*args, **kwargs)) # use the free update to set keys def __getitem__(self, key): @@ -74,7 +75,6 @@ class LRUCache(collections.MutableMapping): # O(m) now = time.time() c = 0 - values_closed = list() # list is cheaper to create while len(self._last_visits) > 0: least = self._last_visits[0] if now - least <= self.timeout: @@ -84,9 +84,9 @@ class LRUCache(collections.MutableMapping): if key in self._store: if now - self._keys_to_last_time[key] > self.timeout: value = self._store[key] - if value not in values_closed: + if value not in self._closed_values: self.close_callback(value) - values_closed.append(value) + self._closed_values.add(value) for key in self._time_to_keys[least]: self._last_visits.popleft() if key in self._store: @@ -96,6 +96,7 @@ class LRUCache(collections.MutableMapping): c += 1 del self._time_to_keys[least] if c: + self._closed_values.clear() logging.debug('%d keys swept' % c)