Python port of ShadowsocksR
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.2 KiB

11 years ago
#!/usr/bin/python
# -*- coding: utf-8 -*-
10 years ago
# Copyright (c) 2014 clowwindy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import absolute_import, division, print_function, \
with_statement
11 years ago
import collections
import logging
import time
# this LRUCache is optimized for concurrency, not QPS
# n: concurrency, keys stored in the cache
# m: visits not timed out, proportional to QPS * timeout
# get & set is O(1), not O(n). thus we can support very large n
# TODO: if timeout or QPS is too large, then this cache is not very efficient,
# as sweep() causes long pause
11 years ago
class LRUCache(collections.MutableMapping):
"""This class is not thread safe"""
11 years ago
def __init__(self, timeout=60, close_callback=None, *args, **kwargs):
11 years ago
self.timeout = timeout
11 years ago
self.close_callback = close_callback
11 years ago
self._store = {}
self._time_to_keys = collections.defaultdict(list)
self._keys_to_last_time = {}
self._last_visits = collections.deque()
11 years ago
self.update(dict(*args, **kwargs)) # use the free update to set keys
def __getitem__(self, key):
# O(1)
11 years ago
t = time.time()
self._keys_to_last_time[key] = t
11 years ago
self._time_to_keys[t].append(key)
self._last_visits.append(t)
11 years ago
return self._store[key]
11 years ago
def __setitem__(self, key, value):
# O(1)
11 years ago
t = time.time()
self._keys_to_last_time[key] = t
11 years ago
self._store[key] = value
self._time_to_keys[t].append(key)
self._last_visits.append(t)
11 years ago
def __delitem__(self, key):
11 years ago
# O(1)
del self._store[key]
del self._keys_to_last_time[key]
11 years ago
def __iter__(self):
11 years ago
return iter(self._store)
11 years ago
def __len__(self):
11 years ago
return len(self._store)
11 years ago
def sweep(self):
11 years ago
# O(m)
11 years ago
now = time.time()
c = 0
11 years ago
while len(self._last_visits) > 0:
least = self._last_visits[0]
11 years ago
if now - least <= self.timeout:
break
if self.close_callback is not None:
for key in self._time_to_keys[least]:
10 years ago
if key in self._store:
if now - self._keys_to_last_time[key] > self.timeout:
value = self._store[key]
self.close_callback(value)
11 years ago
for key in self._time_to_keys[least]:
self._last_visits.popleft()
10 years ago
if key in self._store:
if now - self._keys_to_last_time[key] > self.timeout:
del self._store[key]
10 years ago
del self._keys_to_last_time[key]
c += 1
11 years ago
del self._time_to_keys[least]
11 years ago
if c:
logging.debug('%d keys swept' % c)
def test():
c = LRUCache(timeout=0.3)
c['a'] = 1
assert c['a'] == 1
time.sleep(0.5)
c.sweep()
assert 'a' not in c
c['a'] = 2
c['b'] = 3
time.sleep(0.2)
c.sweep()
assert c['a'] == 2
assert c['b'] == 3
time.sleep(0.2)
c.sweep()
c['b']
time.sleep(0.2)
c.sweep()
assert 'a' not in c
assert c['b'] == 3
time.sleep(0.5)
c.sweep()
assert 'a' not in c
assert 'b' not in c
if __name__ == '__main__':
test()