~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fifo_cache.py

  • Committer: Ian Clatworthy
  • Date: 2008-12-15 04:37:10 UTC
  • mfrom: (3892.1.6 bzr.help-formats)
  • mto: This revision was merged to the branch mainline in revision 3905.
  • Revision ID: ian.clatworthy@canonical.com-20081215043710-ybhxvqjeir13k5ht
Improved help on storage formats (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""A simple first-in-first-out (FIFO) cache."""
18
18
 
73
73
        if len(self) > self._max_cache:
74
74
            self.cleanup()
75
75
 
76
 
    def cache_size(self):
77
 
        """Get the number of entries we will cache."""
78
 
        return self._max_cache
79
 
 
80
76
    def cleanup(self):
81
77
        """Clear the cache until it shrinks to the requested size.
82
78
 
112
108
        key = self._queue.popleft()
113
109
        self._remove(key)
114
110
 
115
 
    def resize(self, max_cache, after_cleanup_count=None):
116
 
        """Increase/decrease the number of cached entries.
117
 
 
118
 
        :param max_cache: The maximum number of entries to cache.
119
 
        :param after_cleanup_count: After cleanup, we should have at most this
120
 
            many entries. This defaults to 80% of max_cache.
121
 
        """
122
 
        self._max_cache = max_cache
123
 
        if after_cleanup_count is None:
124
 
            self._after_cleanup_count = max_cache * 8 / 10
125
 
        else:
126
 
            self._after_cleanup_count = min(max_cache, after_cleanup_count)
127
 
        if len(self) > self._max_cache:
128
 
            self.cleanup()
129
 
 
130
111
    # raise NotImplementedError on dict functions that would mutate the cache
131
112
    # which have not been properly implemented yet.
132
113
    def copy(self):
230
211
            # Time to cleanup
231
212
            self.cleanup()
232
213
 
233
 
    def cache_size(self):
234
 
        """Get the number of bytes we will cache."""
235
 
        return self._max_size
236
 
 
237
214
    def cleanup(self):
238
215
        """Clear the cache until it shrinks to the requested size.
239
216
 
249
226
        val = FIFOCache._remove(self, key)
250
227
        self._value_size -= self._compute_size(val)
251
228
        return val
252
 
 
253
 
    def resize(self, max_size, after_cleanup_size=None):
254
 
        """Increase/decrease the amount of cached data.
255
 
 
256
 
        :param max_size: The maximum number of bytes to cache.
257
 
        :param after_cleanup_size: After cleanup, we should have at most this
258
 
            many bytes cached. This defaults to 80% of max_size.
259
 
        """
260
 
        FIFOCache.resize(self, max_size)
261
 
        self._max_size = max_size
262
 
        if after_cleanup_size is None:
263
 
            self._after_cleanup_size = max_size * 8 / 10
264
 
        else:
265
 
            self._after_cleanup_size = min(max_size, after_cleanup_size)
266
 
        if self._value_size > self._max_size:
267
 
            self.cleanup()
268