~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fifo_cache.py

  • Committer: John Arbash Meinel
  • Date: 2008-12-09 22:26:40 UTC
  • mto: This revision was merged to the branch mainline in revision 3888.
  • Revision ID: john@arbash-meinel.com-20081209222640-u3iece2ixcd0q7lj
Add a FIFOSizeCache which is constrained based on the size of the values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
        self.add(key, value, cleanup=None)
39
39
 
40
40
    def __delitem__(self, key):
 
41
        # Remove the key from an arbitrary location in the queue
41
42
        self._queue.remove(key)
42
43
        self._remove(key)
43
44
 
58
59
            del self[key]
59
60
        self._queue.append(key)
60
61
        dict.__setitem__(self, key, value)
 
62
        if cleanup is not None:
 
63
            self._cleanup[key] = cleanup
61
64
        if len(self) > self._max_cache:
62
65
            self.cleanup()
63
 
        if cleanup is not None:
64
 
            self._cleanup[key] = cleanup
65
66
 
66
67
    def cleanup(self):
67
68
        """Clear the cache until it shrinks to the requested size.
138
139
        if kwargs:
139
140
            for key, val in kwargs.iteritems():
140
141
                self.add(key, val)
 
142
 
 
143
 
 
144
class FIFOSizeCache(FIFOCache):
 
145
    """An FIFOCache that removes things based on the size of the values.
 
146
 
 
147
    This differs in that it doesn't care how many actual items there are,
 
148
    it restricts the cache to be cleaned based on the size of the data.
 
149
    """
 
150
 
 
151
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
 
152
                 compute_size=None):
 
153
        """Create a new FIFOSizeCache.
 
154
 
 
155
        :param max_size: The max number of bytes to store before we start
 
156
            clearing out entries.
 
157
        :param after_cleanup_size: After cleaning up, shrink everything to this
 
158
            size (defaults to 80% of max_size).
 
159
        :param compute_size: A function to compute the size of a value. If
 
160
            not supplied we default to 'len'.
 
161
        """
 
162
        # Arbitrary, we won't really be using the value anyway.
 
163
        FIFOCache.__init__(self, max_cache=max_size)
 
164
        self._max_size = max_size
 
165
        if after_cleanup_size is None:
 
166
            self._after_cleanup_size = self._max_size * 8 / 10
 
167
        else:
 
168
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
 
169
 
 
170
        self._value_size = 0
 
171
        self._compute_size = compute_size
 
172
        if compute_size is None:
 
173
            self._compute_size = len
 
174
 
 
175
    def add(self, key, value, cleanup=None):
 
176
        """Add a new value to the cache.
 
177
 
 
178
        Also, if the entry is ever removed from the queue, call cleanup.
 
179
        Passing it the key and value being removed.
 
180
 
 
181
        :param key: The key to store it under
 
182
        :param value: The object to store, this value by itself is >=
 
183
            after_cleanup_size, then we will not store it at all.
 
184
        :param cleanup: None or a function taking (key, value) to indicate
 
185
                        'value' sohuld be cleaned up.
 
186
        """
 
187
        # Even if the new value won't be stored, we need to remove the old
 
188
        # value
 
189
        if key in self:
 
190
            # Remove the earlier reference to this key, adding it again bumps
 
191
            # it to the end of the queue
 
192
            del self[key]
 
193
        value_len = self._compute_size(value)
 
194
        if value_len >= self._after_cleanup_size:
 
195
            return
 
196
        self._queue.append(key)
 
197
        dict.__setitem__(self, key, value)
 
198
        if cleanup is not None:
 
199
            self._cleanup[key] = cleanup
 
200
        self._value_size += value_len
 
201
        if self._value_size > self._max_size:
 
202
            # Time to cleanup
 
203
            self.cleanup()
 
204
 
 
205
    def cleanup(self):
 
206
        """Clear the cache until it shrinks to the requested size.
 
207
 
 
208
        This does not completely wipe the cache, just makes sure it is under
 
209
        the after_cleanup_size.
 
210
        """
 
211
        # Make sure the cache is shrunk to the correct size
 
212
        while self._value_size > self._after_cleanup_size:
 
213
            self._remove_oldest()
 
214
 
 
215
    def _remove(self, key):
 
216
        """Remove an entry, making sure to maintain the invariants."""
 
217
        val = FIFOCache._remove(self, key)
 
218
        self._value_size -= self._compute_size(val)
 
219
        return val