25
28
class _LRUNode(object):
26
29
"""This maintains the linked-list which is the lru internals."""
28
__slots__ = ('prev', 'next_key', 'key', 'value', 'cleanup', 'size')
31
__slots__ = ('prev', 'next_key', 'key', 'value')
30
def __init__(self, key, value, cleanup=None):
33
def __init__(self, key, value):
32
35
self.next_key = _null_key
35
self.cleanup = cleanup
36
# TODO: We could compute this 'on-the-fly' like we used to, and remove
37
# one pointer from this object, we just need to decide if it
38
# actually costs us much of anything in normal usage
41
39
def __repr__(self):
42
40
if self.prev is None:
46
44
return '%s(%r n:%r p:%r)' % (self.__class__.__name__, self.key,
47
45
self.next_key, prev_key)
49
def run_cleanup(self):
51
if self.cleanup is not None:
52
self.cleanup(self.key, self.value)
54
# cleanup might raise an exception, but we want to make sure
55
# to break refcycles, etc
60
48
class LRUCache(object):
61
49
"""A class which manages a cache of entries, removing unused ones."""
105
93
def __len__(self):
106
94
return len(self._cache)
109
"""Walk the LRU list, only meant to be used in tests."""
110
node = self._most_recently_used
112
if node.prev is not None:
113
raise AssertionError('the _most_recently_used entry is not'
114
' supposed to have a previous entry'
116
while node is not None:
117
if node.next_key is _null_key:
118
if node is not self._least_recently_used:
119
raise AssertionError('only the last node should have'
120
' no next value: %s' % (node,))
123
node_next = self._cache[node.next_key]
124
if node_next.prev is not node:
125
raise AssertionError('inconsistency found, node.next.prev'
126
' != node: %s' % (node,))
127
if node.prev is None:
128
if node is not self._most_recently_used:
129
raise AssertionError('only the _most_recently_used should'
130
' not have a previous node: %s'
133
if node.prev.next_key != node.key:
134
raise AssertionError('inconsistency found, node.prev.next'
135
' != node: %s' % (node,))
96
@symbol_versioning.deprecated_method(
97
symbol_versioning.deprecated_in((2, 5, 0)))
139
98
def add(self, key, value, cleanup=None):
140
"""Add a new value to the cache.
142
Also, if the entry is ever removed from the cache, call
145
:param key: The key to store it under
146
:param value: The object to store
147
:param cleanup: None or a function taking (key, value) to indicate
148
'value' should be cleaned up.
99
if cleanup is not None:
100
raise ValueError("Per-node cleanup functions no longer supported")
101
return self.__setitem__(key, value)
103
def __setitem__(self, key, value):
104
"""Add a new value to the cache"""
150
105
if key is _null_key:
151
106
raise ValueError('cannot use _null_key as a key')
152
107
if key in self._cache:
153
108
node = self._cache[key]
157
# Maintain the LRU properties, even if cleanup raises an
160
node.cleanup = cleanup
161
self._record_access(node)
110
self._record_access(node)
163
node = _LRUNode(key, value, cleanup=cleanup)
112
node = _LRUNode(key, value)
164
113
self._cache[key] = node
165
114
self._record_access(node)
191
140
return self._cache.keys()
194
"""Get the key:value pairs as a dict."""
143
"""Get a new dict with the same key:value pairs as the cache"""
195
144
return dict((k, n.value) for k, n in self._cache.iteritems())
146
items = symbol_versioning.deprecated_method(
147
symbol_versioning.deprecated_in((2, 5, 0)))(as_dict)
197
149
def cleanup(self):
198
150
"""Clear the cache until it shrinks to the requested size.
204
156
while len(self._cache) > self._after_cleanup_count:
205
157
self._remove_lru()
207
def __setitem__(self, key, value):
208
"""Add a value to the cache, there will be no cleanup function."""
209
self.add(key, value, cleanup=None)
211
159
def _record_access(self, node):
212
160
"""Record that key was accessed."""
213
161
# Move 'node' to the front of the queue
241
189
# If we have removed all entries, remove the head pointer as well
242
190
if self._least_recently_used is None:
243
191
self._most_recently_used = None
247
# cleanup might raise an exception, but we want to make sure to
248
# maintain the linked list
249
if node.prev is not None:
250
node.prev.next_key = node.next_key
251
if node.next_key is not _null_key:
252
node_next = self._cache[node.next_key]
253
node_next.prev = node.prev
254
# And remove this node's pointers
256
node.next_key = _null_key
192
if node.prev is not None:
193
node.prev.next_key = node.next_key
194
if node.next_key is not _null_key:
195
node_next = self._cache[node.next_key]
196
node_next.prev = node.prev
197
# And remove this node's pointers
199
node.next_key = _null_key
258
201
def _remove_lru(self):
259
202
"""Remove one entry from the lru, and handle consequences.
316
259
self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
317
260
LRUCache.__init__(self, max_cache=max(int(max_size/512), 1))
319
def add(self, key, value, cleanup=None):
320
"""Add a new value to the cache.
322
Also, if the entry is ever removed from the cache, call
325
:param key: The key to store it under
326
:param value: The object to store
327
:param cleanup: None or a function taking (key, value) to indicate
328
'value' should be cleaned up.
262
def __setitem__(self, key, value):
263
"""Add a new value to the cache"""
330
264
if key is _null_key:
331
265
raise ValueError('cannot use _null_key as a key')
332
266
node = self._cache.get(key, None)
341
275
if node is not None:
342
276
# We won't be replacing the old node, so just remove it
343
277
self._remove_node(node)
344
if cleanup is not None:
348
node = _LRUNode(key, value, cleanup=cleanup)
280
node = _LRUNode(key, value)
349
281
self._cache[key] = node
351
self._value_size -= node.size
352
node.size = value_len
283
self._value_size -= self._compute_size(node.value)
353
284
self._value_size += value_len
354
285
self._record_access(node)
368
299
self._remove_lru()
370
301
def _remove_node(self, node):
371
self._value_size -= node.size
302
self._value_size -= self._compute_size(node.value)
372
303
LRUCache._remove_node(self, node)
374
305
def resize(self, max_size, after_cleanup_size=None):