/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: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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 __future__ import absolute_import
 
20
 
19
21
from . import (
20
22
    trace,
21
23
    )
 
24
from .sixish import (
 
25
    viewitems,
 
26
    viewkeys,
 
27
    )
22
28
 
23
29
 
24
30
_null_key = object()
25
31
 
26
 
 
27
32
class _LRUNode(object):
28
33
    """This maintains the linked-list which is the lru internals."""
29
34
 
130
135
        :return: An unordered list of keys that are currently cached.
131
136
        """
132
137
        # GZ 2016-06-04: Maybe just make this return the view?
133
 
        return list(self._cache.keys())
 
138
        return list(viewkeys(self._cache))
134
139
 
135
140
    def as_dict(self):
136
141
        """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())
 
142
        return dict((k, n.value) for k, n in viewitems(self._cache))
138
143
 
139
144
    def cleanup(self):
140
145
        """Clear the cache until it shrinks to the requested size.
210
215
    def _update_max_cache(self, max_cache, after_cleanup_count=None):
211
216
        self._max_cache = max_cache
212
217
        if after_cleanup_count is None:
213
 
            self._after_cleanup_count = self._max_cache * 8 // 10
 
218
            self._after_cleanup_count = self._max_cache * 8 / 10
214
219
        else:
215
220
            self._after_cleanup_count = min(after_cleanup_count,
216
221
                                            self._max_cache)
227
232
    defaults to len() if not supplied.
228
233
    """
229
234
 
230
 
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
 
235
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
231
236
                 compute_size=None):
232
237
        """Create a new LRUSizeCache.
233
238
 
247
252
        if compute_size is None:
248
253
            self._compute_size = len
249
254
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
250
 
        LRUCache.__init__(self, max_cache=max(int(max_size // 512), 1))
 
255
        LRUCache.__init__(self, max_cache=max(int(max_size/512), 1))
251
256
 
252
257
    def __setitem__(self, key, value):
253
258
        """Add a new value to the cache"""
295
300
    def resize(self, max_size, after_cleanup_size=None):
296
301
        """Change the number of bytes that will be cached."""
297
302
        self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
298
 
        max_cache = max(int(max_size // 512), 1)
 
303
        max_cache = max(int(max_size/512), 1)
299
304
        self._update_max_cache(max_cache)
300
305
 
301
306
    def _update_max_size(self, max_size, after_cleanup_size=None):
302
307
        self._max_size = max_size
303
308
        if after_cleanup_size is None:
304
 
            self._after_cleanup_size = self._max_size * 8 // 10
 
309
            self._after_cleanup_size = self._max_size * 8 / 10
305
310
        else:
306
311
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)