/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 bzrlib/lru_cache.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""A simple least-recently-used (LRU) cache."""
18
18
 
19
 
from . import (
 
19
from __future__ import absolute_import
 
20
 
 
21
from bzrlib import (
 
22
    symbol_versioning,
20
23
    trace,
21
24
    )
22
25
 
23
 
 
24
26
_null_key = object()
25
27
 
26
 
 
27
28
class _LRUNode(object):
28
29
    """This maintains the linked-list which is the lru internals."""
29
30
 
92
93
    def __len__(self):
93
94
        return len(self._cache)
94
95
 
 
96
    @symbol_versioning.deprecated_method(
 
97
        symbol_versioning.deprecated_in((2, 5, 0)))
 
98
    def add(self, key, value, cleanup=None):
 
99
        if cleanup is not None:
 
100
            raise ValueError("Per-node cleanup functions no longer supported")
 
101
        return self.__setitem__(key, value)
 
102
 
95
103
    def __setitem__(self, key, value):
96
104
        """Add a new value to the cache"""
97
105
        if key is _null_key:
129
137
 
130
138
        :return: An unordered list of keys that are currently cached.
131
139
        """
132
 
        # GZ 2016-06-04: Maybe just make this return the view?
133
 
        return list(self._cache.keys())
 
140
        return self._cache.keys()
134
141
 
135
142
    def as_dict(self):
136
143
        """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())
 
144
        return dict((k, n.value) for k, n in self._cache.iteritems())
 
145
 
 
146
    items = symbol_versioning.deprecated_method(
 
147
        symbol_versioning.deprecated_in((2, 5, 0)))(as_dict)
138
148
 
139
149
    def cleanup(self):
140
150
        """Clear the cache until it shrinks to the requested size.
210
220
    def _update_max_cache(self, max_cache, after_cleanup_count=None):
211
221
        self._max_cache = max_cache
212
222
        if after_cleanup_count is None:
213
 
            self._after_cleanup_count = self._max_cache * 8 // 10
 
223
            self._after_cleanup_count = self._max_cache * 8 / 10
214
224
        else:
215
225
            self._after_cleanup_count = min(after_cleanup_count,
216
226
                                            self._max_cache)
227
237
    defaults to len() if not supplied.
228
238
    """
229
239
 
230
 
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
 
240
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
231
241
                 compute_size=None):
232
242
        """Create a new LRUSizeCache.
233
243
 
247
257
        if compute_size is None:
248
258
            self._compute_size = len
249
259
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
250
 
        LRUCache.__init__(self, max_cache=max(int(max_size // 512), 1))
 
260
        LRUCache.__init__(self, max_cache=max(int(max_size/512), 1))
251
261
 
252
262
    def __setitem__(self, key, value):
253
263
        """Add a new value to the cache"""
295
305
    def resize(self, max_size, after_cleanup_size=None):
296
306
        """Change the number of bytes that will be cached."""
297
307
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
298
 
        max_cache = max(int(max_size // 512), 1)
 
308
        max_cache = max(int(max_size/512), 1)
299
309
        self._update_max_cache(max_cache)
300
310
 
301
311
    def _update_max_size(self, max_size, after_cleanup_size=None):
302
312
        self._max_size = max_size
303
313
        if after_cleanup_size is None:
304
 
            self._after_cleanup_size = self._max_size * 8 // 10
 
314
            self._after_cleanup_size = self._max_size * 8 / 10
305
315
        else:
306
316
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)