1
# Copyright (C) 2006 Canonical Ltd
 
 
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.
 
 
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.
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
"""A simple least-recently-used (LRU) cache."""
 
 
19
from collections import deque
 
 
23
class LRUCache(object):
 
 
24
    """A class which manages a cache of entries, removing unused ones."""
 
 
26
    def __init__(self, max_cache=100, after_cleanup_size=None):
 
 
27
        self._max_cache = max_cache
 
 
28
        if after_cleanup_size is None:
 
 
29
            self._after_cleanup_size = self._max_cache
 
 
31
            self._after_cleanup_size = min(after_cleanup_size, self._max_cache)
 
 
33
        self._compact_queue_length = 4*self._max_cache
 
 
37
        self._queue = deque() # Track when things are accessed
 
 
38
        self._refcount = {} # number of entries in self._queue for each key
 
 
40
    def __contains__(self, key):
 
 
41
        return key in self._cache
 
 
43
    def __getitem__(self, key):
 
 
44
        val = self._cache[key]
 
 
45
        self._record_access(key)
 
 
49
        return len(self._cache)
 
 
51
    def add(self, key, value, cleanup=None):
 
 
52
        """Add a new value to the cache.
 
 
54
        Also, if the entry is ever removed from the queue, call cleanup.
 
 
55
        Passing it the key and value being removed.
 
 
57
        :param key: The key to store it under
 
 
58
        :param value: The object to store
 
 
59
        :param cleanup: None or a function taking (key, value) to indicate
 
 
60
                        'value' sohuld be cleaned up.
 
 
62
        if key in self._cache:
 
 
64
        self._cache[key] = value
 
 
65
        self._cleanup[key] = cleanup
 
 
66
        self._record_access(key)
 
 
68
        if len(self._cache) > self._max_cache:
 
 
72
    def get(self, key, default=None):
 
 
73
        if key in self._cache:
 
 
78
        """Clear the cache until it shrinks to the requested size.
 
 
80
        This does not completely wipe the cache, just makes sure it is under
 
 
81
        the after_cleanup_size.
 
 
83
        # Make sure the cache is shrunk to the correct size
 
 
84
        while len(self._cache) > self._after_cleanup_size:
 
 
87
    def __setitem__(self, key, value):
 
 
88
        """Add a value to the cache, there will be no cleanup function."""
 
 
89
        self.add(key, value, cleanup=None)
 
 
91
    def _record_access(self, key):
 
 
92
        """Record that key was accessed."""
 
 
93
        self._queue.append(key)
 
 
94
        # Can't use setdefault because you can't += 1 the result
 
 
95
        self._refcount[key] = self._refcount.get(key, 0) + 1
 
 
97
        # If our access queue is too large, clean it up too
 
 
98
        if len(self._queue) > self._compact_queue_length:
 
 
101
    def _compact_queue(self):
 
 
102
        """Compact the queue, leaving things in sorted last appended order."""
 
 
104
        for item in self._queue:
 
 
105
            if self._refcount[item] == 1:
 
 
106
                new_queue.append(item)
 
 
108
                self._refcount[item] -= 1
 
 
109
        self._queue = new_queue
 
 
110
        # All entries should be of the same size. There should be one entry in
 
 
111
        # queue for each entry in cache, and all refcounts should == 1
 
 
112
        assert (len(self._queue) == len(self._cache) ==
 
 
113
                len(self._refcount) == sum(self._refcount.itervalues()))
 
 
115
    def _remove(self, key):
 
 
116
        """Remove an entry, making sure to maintain the invariants."""
 
 
117
        cleanup = self._cleanup.pop(key)
 
 
118
        val = self._cache.pop(key)
 
 
119
        if cleanup is not None:
 
 
123
    def _remove_lru(self):
 
 
124
        """Remove one entry from the lru, and handle consequences.
 
 
126
        If there are no more references to the lru, then this entry should be
 
 
127
        removed from the cache.
 
 
129
        key = self._queue.popleft()
 
 
130
        self._refcount[key] -= 1
 
 
131
        if not self._refcount[key]:
 
 
132
            del self._refcount[key]
 
 
136
        """Clear out all of the cache."""
 
 
137
        # Clean up in LRU order
 
 
142
class LRUSizeCache(LRUCache):
 
 
143
    """An LRUCache that removes things based on the size of the values.
 
 
145
    This differs in that it doesn't care how many actual items there are,
 
 
146
    it just restricts the cache to be cleaned up after so much data is stored.
 
 
148
    The values that are added must support len(value).
 
 
151
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
 
 
153
        """Create a new LRUSizeCache.
 
 
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
 
 
159
        :param compute_size: A function to compute the size of the values. We
 
 
160
            use a function here, so that you can pass 'len' if you are just
 
 
161
            using simple strings, or a more complex function if you are using
 
 
162
            something like a list of strings, or even a custom object.
 
 
163
            The function should take the form "compute_size(value) => integer".
 
 
164
            If not supplied, it defaults to 'len()'
 
 
166
        # This approximates that texts are > 0.5k in size. It only really
 
 
167
        # effects when we clean up the queue, so we don't want it to be too
 
 
169
        LRUCache.__init__(self, max_cache=int(max_size/512))
 
 
170
        self._max_size = max_size
 
 
171
        if after_cleanup_size is None:
 
 
172
            self._after_cleanup_size = self._max_size
 
 
174
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
 
 
177
        self._compute_size = compute_size
 
 
178
        if compute_size is None:
 
 
179
            self._compute_size = len
 
 
181
    def add(self, key, value, cleanup=None):
 
 
182
        """Add a new value to the cache.
 
 
184
        Also, if the entry is ever removed from the queue, call cleanup.
 
 
185
        Passing it the key and value being removed.
 
 
187
        :param key: The key to store it under
 
 
188
        :param value: The object to store
 
 
189
        :param cleanup: None or a function taking (key, value) to indicate
 
 
190
                        'value' sohuld be cleaned up.
 
 
192
        if key in self._cache:
 
 
194
        value_len = self._compute_size(value)
 
 
195
        if value_len >= self._after_cleanup_size:
 
 
197
        self._value_size += value_len
 
 
198
        self._cache[key] = value
 
 
199
        self._cleanup[key] = cleanup
 
 
200
        self._record_access(key)
 
 
202
        if self._value_size > self._max_size:
 
 
207
        """Clear the cache until it shrinks to the requested size.
 
 
209
        This does not completely wipe the cache, just makes sure it is under
 
 
210
        the after_cleanup_size.
 
 
212
        # Make sure the cache is shrunk to the correct size
 
 
213
        while self._value_size > self._after_cleanup_size:
 
 
216
    def _remove(self, key):
 
 
217
        """Remove an entry, making sure to maintain the invariants."""
 
 
218
        val = LRUCache._remove(self, key)
 
 
219
        self._value_size -= self._compute_size(val)