|
| 1 | +# urllib3/_collections.py |
| 2 | +# Copyright 2008-2011 Andrey Petrov and contributors (see CONTRIBUTORS.txt) |
| 3 | +# |
| 4 | +# This module is part of urllib3 and is released under |
| 5 | +# the MIT License: http://www.opensource.org/licenses/mit-license.php |
| 6 | + |
| 7 | +from collections import deque |
| 8 | + |
| 9 | +from threading import RLock |
| 10 | + |
| 11 | +__all__ = ['RecentlyUsedContainer'] |
| 12 | + |
| 13 | + |
| 14 | +class AccessEntry(object): |
| 15 | + __slots__ = ('key', 'is_valid') |
| 16 | + |
| 17 | + def __init__(self, key, is_valid=True): |
| 18 | + self.key = key |
| 19 | + self.is_valid = is_valid |
| 20 | + |
| 21 | + |
| 22 | +class RecentlyUsedContainer(dict): |
| 23 | + """ |
| 24 | + Provides a dict-like that maintains up to ``maxsize`` keys while throwing |
| 25 | + away the least-recently-used keys beyond ``maxsize``. |
| 26 | + """ |
| 27 | + |
| 28 | + # If len(self.access_log) exceeds self._maxsize * CLEANUP_FACTOR, then we |
| 29 | + # will attempt to cleanup the invalidated entries in the access_log |
| 30 | + # datastructure during the next 'get' operation. |
| 31 | + CLEANUP_FACTOR = 10 |
| 32 | + |
| 33 | + def __init__(self, maxsize=10): |
| 34 | + self._maxsize = maxsize |
| 35 | + |
| 36 | + self._container = {} |
| 37 | + |
| 38 | + # We use a deque to to store our keys ordered by the last access. |
| 39 | + self.access_log = deque() |
| 40 | + self.access_log_lock = RLock() |
| 41 | + |
| 42 | + # We look up the access log entry by the key to invalidate it so we can |
| 43 | + # insert a new authorative entry at the head without having to dig and |
| 44 | + # find the old entry for removal immediately. |
| 45 | + self.access_lookup = {} |
| 46 | + |
| 47 | + # Trigger a heap cleanup when we get past this size |
| 48 | + self.access_log_limit = maxsize * self.CLEANUP_FACTOR |
| 49 | + |
| 50 | + def _invalidate_entry(self, key): |
| 51 | + "If exists: Invalidate old entry and return it." |
| 52 | + old_entry = self.access_lookup.get(key) |
| 53 | + if old_entry: |
| 54 | + old_entry.is_valid = False |
| 55 | + |
| 56 | + return old_entry |
| 57 | + |
| 58 | + def _push_entry(self, key): |
| 59 | + "Push entry onto our access log, invalidate the old entry if exists." |
| 60 | + self._invalidate_entry(key) |
| 61 | + |
| 62 | + new_entry = AccessEntry(key) |
| 63 | + self.access_lookup[key] = new_entry |
| 64 | + |
| 65 | + self.access_log_lock.acquire() |
| 66 | + self.access_log.appendleft(new_entry) |
| 67 | + self.access_log_lock.release() |
| 68 | + |
| 69 | + def _prune_entries(self, num): |
| 70 | + "Pop entries from our access log until we popped ``num`` valid ones." |
| 71 | + while num > 0: |
| 72 | + self.access_log_lock.acquire() |
| 73 | + p = self.access_log.pop() |
| 74 | + self.access_log_lock.release() |
| 75 | + |
| 76 | + if not p.is_valid: |
| 77 | + continue # Invalidated entry, skip |
| 78 | + |
| 79 | + dict.pop(self, p.key, None) |
| 80 | + self.access_lookup.pop(p.key, None) |
| 81 | + num -= 1 |
| 82 | + |
| 83 | + def _prune_invalidated_entries(self): |
| 84 | + "Rebuild our access_log without the invalidated entries." |
| 85 | + self.access_log_lock.acquire() |
| 86 | + self.access_log = deque(e for e in self.access_log if e.is_valid) |
| 87 | + self.access_log_lock.release() |
| 88 | + |
| 89 | + def _get_ordered_access_keys(self): |
| 90 | + "Return ordered access keys for inspection. Used for testing." |
| 91 | + self.access_log_lock.acquire() |
| 92 | + r = [e.key for e in self.access_log if e.is_valid] |
| 93 | + self.access_log_lock.release() |
| 94 | + |
| 95 | + return r |
| 96 | + |
| 97 | + def __getitem__(self, key): |
| 98 | + item = dict.get(self, key) |
| 99 | + |
| 100 | + if not item: |
| 101 | + raise KeyError(key) |
| 102 | + |
| 103 | + # Insert new entry with new high priority, also implicitly invalidates |
| 104 | + # the old entry. |
| 105 | + self._push_entry(key) |
| 106 | + |
| 107 | + if len(self.access_log) > self.access_log_limit: |
| 108 | + # Heap is getting too big, try to clean up any tailing invalidated |
| 109 | + # entries. |
| 110 | + self._prune_invalidated_entries() |
| 111 | + |
| 112 | + return item |
| 113 | + |
| 114 | + def __setitem__(self, key, item): |
| 115 | + # Add item to our container and access log |
| 116 | + dict.__setitem__(self, key, item) |
| 117 | + self._push_entry(key) |
| 118 | + |
| 119 | + # Discard invalid and excess entries |
| 120 | + self._prune_entries(len(self) - self._maxsize) |
| 121 | + |
| 122 | + def __delitem__(self, key): |
| 123 | + self._invalidate_entry(key) |
| 124 | + self.access_lookup.pop(key, None) |
| 125 | + dict.__delitem__(self, key) |
| 126 | + |
| 127 | + def get(self, key, default=None): |
| 128 | + try: |
| 129 | + return self[key] |
| 130 | + except KeyError: |
| 131 | + return default |
0 commit comments