/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/fifo_cache.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""A simple first-in-first-out (FIFO) cache."""
18
18
 
 
19
from __future__ import absolute_import, division
 
20
 
19
21
from collections import deque
20
22
 
21
23
 
30
32
        else:
31
33
            self._after_cleanup_count = min(after_cleanup_count,
32
34
                                            self._max_cache)
33
 
        self._cleanup = {}  # map to cleanup functions when items are removed
34
 
        self._queue = deque()  # Track when things are accessed
 
35
        self._cleanup = {} # map to cleanup functions when items are removed
 
36
        self._queue = deque() # Track when things are accessed
35
37
 
36
38
    def __setitem__(self, key, value):
37
39
        """Add a value to the cache, there will be no cleanup function."""
39
41
 
40
42
    def __delitem__(self, key):
41
43
        # Remove the key from an arbitrary location in the queue
42
 
        self._queue.remove(key)
 
44
        remove = getattr(self._queue, 'remove', None)
 
45
        # Python2.5's has deque.remove, but Python2.4 does not
 
46
        if remove is not None:
 
47
            remove(key)
 
48
        else:
 
49
            # TODO: It would probably be faster to pop()/popleft() until we get to the
 
50
            #       key, and then insert those back into the queue. We know
 
51
            #       the key should only be present in one position, and we
 
52
            #       wouldn't need to rebuild the whole queue.
 
53
            self._queue = deque([k for k in self._queue if k != key])
43
54
        self._remove(key)
44
55
 
45
56
    def add(self, key, value, cleanup=None):
79
90
            self._remove_oldest()
80
91
        if len(self._queue) != len(self):
81
92
            raise AssertionError('The length of the queue should always equal'
82
 
                                 ' the length of the dict. %s != %s'
83
 
                                 % (len(self._queue), len(self)))
 
93
                ' the length of the dict. %s != %s'
 
94
                % (len(self._queue), len(self)))
84
95
 
85
96
    def clear(self):
86
97
        """Clear out all of the cache."""
167
178
    it restricts the cache to be cleaned based on the size of the data.
168
179
    """
169
180
 
170
 
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
 
181
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
171
182
                 compute_size=None):
172
183
        """Create a new FIFOSizeCache.
173
184
 
256
267
            self._after_cleanup_size = min(max_size, after_cleanup_size)
257
268
        if self._value_size > self._max_size:
258
269
            self.cleanup()
 
270