|
5 | 5 | ''' |
6 | 6 |
|
7 | 7 | import re |
| 8 | +from collections import OrderedDict |
8 | 9 |
|
9 | | -try: |
10 | | - from collections import OrderedDict |
11 | | -except ImportError: |
12 | | - OrderedDict = None |
13 | 10 |
|
| 11 | +class Cache(OrderedDict): |
| 12 | + """Cache with LRU algorithm using an OrderedDict as basis |
| 13 | + """ |
| 14 | + def __init__(self, maxsize=100): |
| 15 | + OrderedDict.__init__(self) |
14 | 16 |
|
15 | | -if OrderedDict: |
16 | | - class Cache(OrderedDict): |
17 | | - """Cache with LRU algorithm using an OrderedDict as basis |
18 | | - """ |
19 | | - def __init__(self, maxsize=100): |
20 | | - OrderedDict.__init__(self) |
21 | | - |
22 | | - self._maxsize = maxsize |
23 | | - |
24 | | - def __getitem__(self, key, *args, **kwargs): |
25 | | - # Get the key and remove it from the cache, or raise KeyError |
26 | | - value = OrderedDict.__getitem__(self, key) |
27 | | - del self[key] |
28 | | - |
29 | | - # Insert the (key, value) pair on the front of the cache |
30 | | - OrderedDict.__setitem__(self, key, value) |
31 | | - |
32 | | - # Return the value from the cache |
33 | | - return value |
34 | | - |
35 | | - def __setitem__(self, key, value, *args, **kwargs): |
36 | | - # Key was inserted before, remove it so we put it at front later |
37 | | - if key in self: |
38 | | - del self[key] |
| 17 | + self._maxsize = maxsize |
39 | 18 |
|
40 | | - # Too much items on the cache, remove the least recent used |
41 | | - elif len(self) >= self._maxsize: |
42 | | - self.popitem(False) |
| 19 | + def __getitem__(self, key, *args, **kwargs): |
| 20 | + # Get the key and remove it from the cache, or raise KeyError |
| 21 | + value = OrderedDict.__getitem__(self, key) |
| 22 | + del self[key] |
43 | 23 |
|
44 | | - # Insert the (key, value) pair on the front of the cache |
45 | | - OrderedDict.__setitem__(self, key, value, *args, **kwargs) |
| 24 | + # Insert the (key, value) pair on the front of the cache |
| 25 | + OrderedDict.__setitem__(self, key, value) |
46 | 26 |
|
47 | | -else: |
48 | | - class Cache(dict): |
49 | | - """Cache that reset when gets full |
50 | | - """ |
51 | | - def __init__(self, maxsize=100): |
52 | | - dict.__init__(self) |
| 27 | + # Return the value from the cache |
| 28 | + return value |
53 | 29 |
|
54 | | - self._maxsize = maxsize |
| 30 | + def __setitem__(self, key, value, *args, **kwargs): |
| 31 | + # Key was inserted before, remove it so we put it at front later |
| 32 | + if key in self: |
| 33 | + del self[key] |
55 | 34 |
|
56 | | - def __setitem__(self, key, value, *args, **kwargs): |
57 | | - # Reset the cache if we have too much cached entries and start over |
58 | | - if len(self) >= self._maxsize: |
59 | | - self.clear() |
| 35 | + # Too much items on the cache, remove the least recent used |
| 36 | + elif len(self) >= self._maxsize: |
| 37 | + self.popitem(False) |
60 | 38 |
|
61 | | - # Insert the (key, value) pair on the front of the cache |
62 | | - dict.__setitem__(self, key, value, *args, **kwargs) |
| 39 | + # Insert the (key, value) pair on the front of the cache |
| 40 | + OrderedDict.__setitem__(self, key, value, *args, **kwargs) |
63 | 41 |
|
64 | 42 |
|
65 | 43 | def memoize_generator(func): |
|
0 commit comments