/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lru_cache.py

  • Committer: Robert Collins
  • Date: 2005-12-24 02:20:45 UTC
  • mto: (1185.50.57 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: robertc@robertcollins.net-20051224022045-14efc8dfa0e1a4e9
Start tests for api usage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""A simple least-recently-used (LRU) cache."""
18
 
 
19
 
from . import (
20
 
    trace,
21
 
    )
22
 
 
23
 
 
24
 
_null_key = object()
25
 
 
26
 
 
27
 
class _LRUNode(object):
28
 
    """This maintains the linked-list which is the lru internals."""
29
 
 
30
 
    __slots__ = ('prev', 'next_key', 'key', 'value')
31
 
 
32
 
    def __init__(self, key, value):
33
 
        self.prev = None
34
 
        self.next_key = _null_key
35
 
        self.key = key
36
 
        self.value = value
37
 
 
38
 
    def __repr__(self):
39
 
        if self.prev is None:
40
 
            prev_key = None
41
 
        else:
42
 
            prev_key = self.prev.key
43
 
        return '%s(%r n:%r p:%r)' % (self.__class__.__name__, self.key,
44
 
                                     self.next_key, prev_key)
45
 
 
46
 
 
47
 
class LRUCache(object):
48
 
    """A class which manages a cache of entries, removing unused ones."""
49
 
 
50
 
    def __init__(self, max_cache=100, after_cleanup_count=None):
51
 
        self._cache = {}
52
 
        # The "HEAD" of the lru linked list
53
 
        self._most_recently_used = None
54
 
        # The "TAIL" of the lru linked list
55
 
        self._least_recently_used = None
56
 
        self._update_max_cache(max_cache, after_cleanup_count)
57
 
 
58
 
    def __contains__(self, key):
59
 
        return key in self._cache
60
 
 
61
 
    def __getitem__(self, key):
62
 
        cache = self._cache
63
 
        node = cache[key]
64
 
        # Inlined from _record_access to decrease the overhead of __getitem__
65
 
        # We also have more knowledge about structure if __getitem__ is
66
 
        # succeeding, then we know that self._most_recently_used must not be
67
 
        # None, etc.
68
 
        mru = self._most_recently_used
69
 
        if node is mru:
70
 
            # Nothing to do, this node is already at the head of the queue
71
 
            return node.value
72
 
        # Remove this node from the old location
73
 
        node_prev = node.prev
74
 
        next_key = node.next_key
75
 
        # benchmarking shows that the lookup of _null_key in globals is faster
76
 
        # than the attribute lookup for (node is self._least_recently_used)
77
 
        if next_key is _null_key:
78
 
            # 'node' is the _least_recently_used, because it doesn't have a
79
 
            # 'next' item. So move the current lru to the previous node.
80
 
            self._least_recently_used = node_prev
81
 
        else:
82
 
            node_next = cache[next_key]
83
 
            node_next.prev = node_prev
84
 
        node_prev.next_key = next_key
85
 
        # Insert this node at the front of the list
86
 
        node.next_key = mru.key
87
 
        mru.prev = node
88
 
        self._most_recently_used = node
89
 
        node.prev = None
90
 
        return node.value
91
 
 
92
 
    def __len__(self):
93
 
        return len(self._cache)
94
 
 
95
 
    def __setitem__(self, key, value):
96
 
        """Add a new value to the cache"""
97
 
        if key is _null_key:
98
 
            raise ValueError('cannot use _null_key as a key')
99
 
        if key in self._cache:
100
 
            node = self._cache[key]
101
 
            node.value = value
102
 
            self._record_access(node)
103
 
        else:
104
 
            node = _LRUNode(key, value)
105
 
            self._cache[key] = node
106
 
            self._record_access(node)
107
 
 
108
 
        if len(self._cache) > self._max_cache:
109
 
            # Trigger the cleanup
110
 
            self.cleanup()
111
 
 
112
 
    def cache_size(self):
113
 
        """Get the number of entries we will cache."""
114
 
        return self._max_cache
115
 
 
116
 
    def get(self, key, default=None):
117
 
        node = self._cache.get(key, None)
118
 
        if node is None:
119
 
            return default
120
 
        self._record_access(node)
121
 
        return node.value
122
 
 
123
 
    def keys(self):
124
 
        """Get the list of keys currently cached.
125
 
 
126
 
        Note that values returned here may not be available by the time you
127
 
        request them later. This is simply meant as a peak into the current
128
 
        state.
129
 
 
130
 
        :return: An unordered list of keys that are currently cached.
131
 
        """
132
 
        # GZ 2016-06-04: Maybe just make this return the view?
133
 
        return list(self._cache.keys())
134
 
 
135
 
    def as_dict(self):
136
 
        """Get a new dict with the same key:value pairs as the cache"""
137
 
        return dict((k, n.value) for k, n in self._cache.items())
138
 
 
139
 
    def cleanup(self):
140
 
        """Clear the cache until it shrinks to the requested size.
141
 
 
142
 
        This does not completely wipe the cache, just makes sure it is under
143
 
        the after_cleanup_count.
144
 
        """
145
 
        # Make sure the cache is shrunk to the correct size
146
 
        while len(self._cache) > self._after_cleanup_count:
147
 
            self._remove_lru()
148
 
 
149
 
    def _record_access(self, node):
150
 
        """Record that key was accessed."""
151
 
        # Move 'node' to the front of the queue
152
 
        if self._most_recently_used is None:
153
 
            self._most_recently_used = node
154
 
            self._least_recently_used = node
155
 
            return
156
 
        elif node is self._most_recently_used:
157
 
            # Nothing to do, this node is already at the head of the queue
158
 
            return
159
 
        # We've taken care of the tail pointer, remove the node, and insert it
160
 
        # at the front
161
 
        # REMOVE
162
 
        if node is self._least_recently_used:
163
 
            self._least_recently_used = node.prev
164
 
        if node.prev is not None:
165
 
            node.prev.next_key = node.next_key
166
 
        if node.next_key is not _null_key:
167
 
            node_next = self._cache[node.next_key]
168
 
            node_next.prev = node.prev
169
 
        # INSERT
170
 
        node.next_key = self._most_recently_used.key
171
 
        self._most_recently_used.prev = node
172
 
        self._most_recently_used = node
173
 
        node.prev = None
174
 
 
175
 
    def _remove_node(self, node):
176
 
        if node is self._least_recently_used:
177
 
            self._least_recently_used = node.prev
178
 
        self._cache.pop(node.key)
179
 
        # If we have removed all entries, remove the head pointer as well
180
 
        if self._least_recently_used is None:
181
 
            self._most_recently_used = None
182
 
        if node.prev is not None:
183
 
            node.prev.next_key = node.next_key
184
 
        if node.next_key is not _null_key:
185
 
            node_next = self._cache[node.next_key]
186
 
            node_next.prev = node.prev
187
 
        # And remove this node's pointers
188
 
        node.prev = None
189
 
        node.next_key = _null_key
190
 
 
191
 
    def _remove_lru(self):
192
 
        """Remove one entry from the lru, and handle consequences.
193
 
 
194
 
        If there are no more references to the lru, then this entry should be
195
 
        removed from the cache.
196
 
        """
197
 
        self._remove_node(self._least_recently_used)
198
 
 
199
 
    def clear(self):
200
 
        """Clear out all of the cache."""
201
 
        # Clean up in LRU order
202
 
        while self._cache:
203
 
            self._remove_lru()
204
 
 
205
 
    def resize(self, max_cache, after_cleanup_count=None):
206
 
        """Change the number of entries that will be cached."""
207
 
        self._update_max_cache(max_cache,
208
 
                               after_cleanup_count=after_cleanup_count)
209
 
 
210
 
    def _update_max_cache(self, max_cache, after_cleanup_count=None):
211
 
        self._max_cache = max_cache
212
 
        if after_cleanup_count is None:
213
 
            self._after_cleanup_count = self._max_cache * 8 // 10
214
 
        else:
215
 
            self._after_cleanup_count = min(after_cleanup_count,
216
 
                                            self._max_cache)
217
 
        self.cleanup()
218
 
 
219
 
 
220
 
class LRUSizeCache(LRUCache):
221
 
    """An LRUCache that removes things based on the size of the values.
222
 
 
223
 
    This differs in that it doesn't care how many actual items there are,
224
 
    it just restricts the cache to be cleaned up after so much data is stored.
225
 
 
226
 
    The size of items added will be computed using compute_size(value), which
227
 
    defaults to len() if not supplied.
228
 
    """
229
 
 
230
 
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
231
 
                 compute_size=None):
232
 
        """Create a new LRUSizeCache.
233
 
 
234
 
        :param max_size: The max number of bytes to store before we start
235
 
            clearing out entries.
236
 
        :param after_cleanup_size: After cleaning up, shrink everything to this
237
 
            size.
238
 
        :param compute_size: A function to compute the size of the values. We
239
 
            use a function here, so that you can pass 'len' if you are just
240
 
            using simple strings, or a more complex function if you are using
241
 
            something like a list of strings, or even a custom object.
242
 
            The function should take the form "compute_size(value) => integer".
243
 
            If not supplied, it defaults to 'len()'
244
 
        """
245
 
        self._value_size = 0
246
 
        self._compute_size = compute_size
247
 
        if compute_size is None:
248
 
            self._compute_size = len
249
 
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
250
 
        LRUCache.__init__(self, max_cache=max(int(max_size // 512), 1))
251
 
 
252
 
    def __setitem__(self, key, value):
253
 
        """Add a new value to the cache"""
254
 
        if key is _null_key:
255
 
            raise ValueError('cannot use _null_key as a key')
256
 
        node = self._cache.get(key, None)
257
 
        value_len = self._compute_size(value)
258
 
        if value_len >= self._after_cleanup_size:
259
 
            # The new value is 'too big to fit', as it would fill up/overflow
260
 
            # the cache all by itself
261
 
            trace.mutter('Adding the key %r to an LRUSizeCache failed.'
262
 
                         ' value %d is too big to fit in a the cache'
263
 
                         ' with size %d %d', key, value_len,
264
 
                         self._after_cleanup_size, self._max_size)
265
 
            if node is not None:
266
 
                # We won't be replacing the old node, so just remove it
267
 
                self._remove_node(node)
268
 
            return
269
 
        if node is None:
270
 
            node = _LRUNode(key, value)
271
 
            self._cache[key] = node
272
 
        else:
273
 
            self._value_size -= self._compute_size(node.value)
274
 
        self._value_size += value_len
275
 
        self._record_access(node)
276
 
 
277
 
        if self._value_size > self._max_size:
278
 
            # Time to cleanup
279
 
            self.cleanup()
280
 
 
281
 
    def cleanup(self):
282
 
        """Clear the cache until it shrinks to the requested size.
283
 
 
284
 
        This does not completely wipe the cache, just makes sure it is under
285
 
        the after_cleanup_size.
286
 
        """
287
 
        # Make sure the cache is shrunk to the correct size
288
 
        while self._value_size > self._after_cleanup_size:
289
 
            self._remove_lru()
290
 
 
291
 
    def _remove_node(self, node):
292
 
        self._value_size -= self._compute_size(node.value)
293
 
        LRUCache._remove_node(self, node)
294
 
 
295
 
    def resize(self, max_size, after_cleanup_size=None):
296
 
        """Change the number of bytes that will be cached."""
297
 
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
298
 
        max_cache = max(int(max_size // 512), 1)
299
 
        self._update_max_cache(max_cache)
300
 
 
301
 
    def _update_max_size(self, max_size, after_cleanup_size=None):
302
 
        self._max_size = max_size
303
 
        if after_cleanup_size is None:
304
 
            self._after_cleanup_size = self._max_size * 8 // 10
305
 
        else:
306
 
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)