bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4516.2.1
by John Arbash Meinel
Fix bug #396838, Update LRUCache to maintain invariant even |
1 |
# Copyright (C) 2006, 2008, 2009 Canonical Ltd
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
16 |
|
17 |
"""Tests for the lru_cache module."""
|
|
18 |
||
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
19 |
from .. import ( |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
20 |
lru_cache, |
21 |
tests, |
|
22 |
)
|
|
23 |
||
24 |
||
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
25 |
def walk_lru(lru): |
26 |
"""Test helper to walk the LRU list and assert its consistency""" |
|
27 |
node = lru._most_recently_used |
|
28 |
if node is not None: |
|
29 |
if node.prev is not None: |
|
30 |
raise AssertionError('the _most_recently_used entry is not' |
|
31 |
' supposed to have a previous entry'
|
|
32 |
' %s' % (node,)) |
|
33 |
while node is not None: |
|
34 |
if node.next_key is lru_cache._null_key: |
|
35 |
if node is not lru._least_recently_used: |
|
36 |
raise AssertionError('only the last node should have' |
|
37 |
' no next value: %s' % (node,)) |
|
38 |
node_next = None |
|
39 |
else: |
|
40 |
node_next = lru._cache[node.next_key] |
|
41 |
if node_next.prev is not node: |
|
42 |
raise AssertionError('inconsistency found, node.next.prev' |
|
43 |
' != node: %s' % (node,)) |
|
44 |
if node.prev is None: |
|
45 |
if node is not lru._most_recently_used: |
|
46 |
raise AssertionError('only the _most_recently_used should' |
|
47 |
' not have a previous node: %s' |
|
48 |
% (node,)) |
|
49 |
else: |
|
50 |
if node.prev.next_key != node.key: |
|
51 |
raise AssertionError('inconsistency found, node.prev.next' |
|
52 |
' != node: %s' % (node,)) |
|
53 |
yield node |
|
54 |
node = node_next |
|
55 |
||
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
56 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
57 |
class TestLRUCache(tests.TestCase): |
58 |
"""Test that LRU cache properly keeps track of entries.""" |
|
59 |
||
|
4178.3.2
by John Arbash Meinel
Add tests for LRUCache.cache_size() |
60 |
def test_cache_size(self): |
61 |
cache = lru_cache.LRUCache(max_cache=10) |
|
62 |
self.assertEqual(10, cache.cache_size()) |
|
63 |
||
64 |
cache = lru_cache.LRUCache(max_cache=256) |
|
65 |
self.assertEqual(256, cache.cache_size()) |
|
66 |
||
67 |
cache.resize(512) |
|
68 |
self.assertEqual(512, cache.cache_size()) |
|
69 |
||
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
70 |
def test_missing(self): |
71 |
cache = lru_cache.LRUCache(max_cache=10) |
|
72 |
||
|
5784.1.1
by Martin Pool
Stop using failIf, failUnless, etc |
73 |
self.assertFalse('foo' in cache) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
74 |
self.assertRaises(KeyError, cache.__getitem__, 'foo') |
75 |
||
76 |
cache['foo'] = 'bar' |
|
77 |
self.assertEqual('bar', cache['foo']) |
|
|
5784.1.1
by Martin Pool
Stop using failIf, failUnless, etc |
78 |
self.assertTrue('foo' in cache) |
79 |
self.assertFalse('bar' in cache) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
80 |
|
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
81 |
def test_map_None(self): |
82 |
# Make sure that we can properly map None as a key.
|
|
83 |
cache = lru_cache.LRUCache(max_cache=10) |
|
|
5784.1.1
by Martin Pool
Stop using failIf, failUnless, etc |
84 |
self.assertFalse(None in cache) |
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
85 |
cache[None] = 1 |
86 |
self.assertEqual(1, cache[None]) |
|
87 |
cache[None] = 2 |
|
88 |
self.assertEqual(2, cache[None]) |
|
89 |
# Test the various code paths of __getitem__, to make sure that we can
|
|
90 |
# handle when None is the key for the LRU and the MRU
|
|
91 |
cache[1] = 3 |
|
92 |
cache[None] = 1 |
|
93 |
cache[None] |
|
94 |
cache[1] |
|
95 |
cache[None] |
|
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
96 |
self.assertEqual([None, 1], [n.key for n in walk_lru(cache)]) |
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
97 |
|
98 |
def test_add__null_key(self): |
|
99 |
cache = lru_cache.LRUCache(max_cache=10) |
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
100 |
self.assertRaises(ValueError, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
101 |
cache.__setitem__, lru_cache._null_key, 1) |
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
102 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
103 |
def test_overflow(self): |
104 |
"""Adding extra entries will pop out old ones.""" |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
105 |
cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
106 |
|
107 |
cache['foo'] = 'bar' |
|
108 |
# With a max cache of 1, adding 'baz' should pop out 'foo'
|
|
109 |
cache['baz'] = 'biz' |
|
110 |
||
|
5784.1.1
by Martin Pool
Stop using failIf, failUnless, etc |
111 |
self.assertFalse('foo' in cache) |
112 |
self.assertTrue('baz' in cache) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
113 |
|
114 |
self.assertEqual('biz', cache['baz']) |
|
115 |
||
116 |
def test_by_usage(self): |
|
117 |
"""Accessing entries bumps them up in priority.""" |
|
118 |
cache = lru_cache.LRUCache(max_cache=2) |
|
119 |
||
120 |
cache['baz'] = 'biz' |
|
121 |
cache['foo'] = 'bar' |
|
122 |
||
123 |
self.assertEqual('biz', cache['baz']) |
|
124 |
||
125 |
# This must kick out 'foo' because it was the last accessed
|
|
126 |
cache['nub'] = 'in' |
|
127 |
||
|
5784.1.1
by Martin Pool
Stop using failIf, failUnless, etc |
128 |
self.assertFalse('foo' in cache) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
129 |
|
130 |
def test_len(self): |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
131 |
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
132 |
|
133 |
cache[1] = 10 |
|
134 |
cache[2] = 20 |
|
135 |
cache[3] = 30 |
|
136 |
cache[4] = 40 |
|
137 |
||
138 |
self.assertEqual(4, len(cache)) |
|
139 |
||
140 |
cache[5] = 50 |
|
141 |
cache[6] = 60 |
|
142 |
cache[7] = 70 |
|
143 |
cache[8] = 80 |
|
144 |
||
145 |
self.assertEqual(8, len(cache)) |
|
146 |
||
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
147 |
cache[1] = 15 # replacement |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
148 |
|
149 |
self.assertEqual(8, len(cache)) |
|
150 |
||
151 |
cache[9] = 90 |
|
152 |
cache[10] = 100 |
|
153 |
cache[11] = 110 |
|
154 |
||
155 |
# We hit the max
|
|
156 |
self.assertEqual(10, len(cache)) |
|
|
4287.1.2
by John Arbash Meinel
Properly remove the nodes from the internal linked list in _remove_node. |
157 |
self.assertEqual([11, 10, 9, 1, 8, 7, 6, 5, 4, 3], |
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
158 |
[n.key for n in walk_lru(cache)]) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
159 |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
160 |
def test_cleanup_shrinks_to_after_clean_count(self): |
161 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
162 |
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
163 |
cache[1] = 10 |
164 |
cache[2] = 20 |
|
165 |
cache[3] = 25 |
|
166 |
cache[4] = 30 |
|
167 |
cache[5] = 35 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
168 |
|
169 |
self.assertEqual(5, len(cache)) |
|
170 |
# This will bump us over the max, which causes us to shrink down to
|
|
171 |
# after_cleanup_cache size
|
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
172 |
cache[6] = 40 |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
173 |
self.assertEqual(3, len(cache)) |
174 |
||
175 |
def test_after_cleanup_larger_than_max(self): |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
176 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10) |
177 |
self.assertEqual(5, cache._after_cleanup_count) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
178 |
|
179 |
def test_after_cleanup_none(self): |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
180 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None) |
181 |
# By default _after_cleanup_size is 80% of the normal size
|
|
182 |
self.assertEqual(4, cache._after_cleanup_count) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
183 |
|
184 |
def test_cleanup(self): |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
185 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
186 |
|
187 |
# Add these in order
|
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
188 |
cache[1] = 10 |
189 |
cache[2] = 20 |
|
190 |
cache[3] = 25 |
|
191 |
cache[4] = 30 |
|
192 |
cache[5] = 35 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
193 |
|
194 |
self.assertEqual(5, len(cache)) |
|
195 |
# Force a compaction
|
|
196 |
cache.cleanup() |
|
197 |
self.assertEqual(2, len(cache)) |
|
198 |
||
|
4178.3.7
by John Arbash Meinel
Review tweaks from Ian. |
199 |
def test_preserve_last_access_order(self): |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
200 |
cache = lru_cache.LRUCache(max_cache=5) |
201 |
||
202 |
# Add these in order
|
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
203 |
cache[1] = 10 |
204 |
cache[2] = 20 |
|
205 |
cache[3] = 25 |
|
206 |
cache[4] = 30 |
|
207 |
cache[5] = 35 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
208 |
|
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
209 |
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in walk_lru(cache)]) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
210 |
|
211 |
# Now access some randomly
|
|
212 |
cache[2] |
|
213 |
cache[5] |
|
214 |
cache[3] |
|
215 |
cache[2] |
|
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
216 |
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in walk_lru(cache)]) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
217 |
|
|
2998.2.1
by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get() |
218 |
def test_get(self): |
219 |
cache = lru_cache.LRUCache(max_cache=5) |
|
220 |
||
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
221 |
cache[1] = 10 |
222 |
cache[2] = 20 |
|
|
2998.2.1
by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get() |
223 |
self.assertEqual(20, cache.get(2)) |
224 |
self.assertIs(None, cache.get(3)) |
|
225 |
obj = object() |
|
226 |
self.assertIs(obj, cache.get(3, obj)) |
|
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
227 |
self.assertEqual([2, 1], [n.key for n in walk_lru(cache)]) |
|
4178.3.5
by John Arbash Meinel
Add tests that LRUCache.get() properly tracks accesses. |
228 |
self.assertEqual(10, cache.get(1)) |
|
6215.1.2
by Martin Packman
Move private test helper LRUCache._walk_lru to test module |
229 |
self.assertEqual([1, 2], [n.key for n in walk_lru(cache)]) |
|
2998.2.1
by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get() |
230 |
|
|
3763.8.10
by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache. |
231 |
def test_keys(self): |
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
232 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5) |
|
3763.8.10
by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache. |
233 |
|
234 |
cache[1] = 2 |
|
235 |
cache[2] = 3 |
|
236 |
cache[3] = 4 |
|
237 |
self.assertEqual([1, 2, 3], sorted(cache.keys())) |
|
238 |
cache[4] = 5 |
|
239 |
cache[5] = 6 |
|
240 |
cache[6] = 7 |
|
241 |
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys())) |
|
242 |
||
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
243 |
def test_resize_smaller(self): |
244 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4) |
|
245 |
cache[1] = 2 |
|
246 |
cache[2] = 3 |
|
247 |
cache[3] = 4 |
|
248 |
cache[4] = 5 |
|
249 |
cache[5] = 6 |
|
250 |
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys())) |
|
251 |
cache[6] = 7 |
|
252 |
self.assertEqual([3, 4, 5, 6], sorted(cache.keys())) |
|
253 |
# Now resize to something smaller, which triggers a cleanup
|
|
254 |
cache.resize(max_cache=3, after_cleanup_count=2) |
|
255 |
self.assertEqual([5, 6], sorted(cache.keys())) |
|
256 |
# Adding something will use the new size
|
|
257 |
cache[7] = 8 |
|
258 |
self.assertEqual([5, 6, 7], sorted(cache.keys())) |
|
259 |
cache[8] = 9 |
|
260 |
self.assertEqual([7, 8], sorted(cache.keys())) |
|
261 |
||
262 |
def test_resize_larger(self): |
|
263 |
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4) |
|
264 |
cache[1] = 2 |
|
265 |
cache[2] = 3 |
|
266 |
cache[3] = 4 |
|
267 |
cache[4] = 5 |
|
268 |
cache[5] = 6 |
|
269 |
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys())) |
|
270 |
cache[6] = 7 |
|
271 |
self.assertEqual([3, 4, 5, 6], sorted(cache.keys())) |
|
272 |
cache.resize(max_cache=8, after_cleanup_count=6) |
|
273 |
self.assertEqual([3, 4, 5, 6], sorted(cache.keys())) |
|
274 |
cache[7] = 8 |
|
275 |
cache[8] = 9 |
|
276 |
cache[9] = 10 |
|
277 |
cache[10] = 11 |
|
278 |
self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys())) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
279 |
cache[11] = 12 # triggers cleanup back to new after_cleanup_count |
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
280 |
self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys())) |
281 |
||
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
282 |
|
283 |
class TestLRUSizeCache(tests.TestCase): |
|
284 |
||
285 |
def test_basic_init(self): |
|
286 |
cache = lru_cache.LRUSizeCache() |
|
287 |
self.assertEqual(2048, cache._max_cache) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
288 |
self.assertEqual(int(cache._max_size * 0.8), cache._after_cleanup_size) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
289 |
self.assertEqual(0, cache._value_size) |
290 |
||
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
291 |
def test_add__null_key(self): |
292 |
cache = lru_cache.LRUSizeCache() |
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
293 |
self.assertRaises(ValueError, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
294 |
cache.__setitem__, lru_cache._null_key, 1) |
|
4287.1.10
by John Arbash Meinel
Restore the ability to handle None as a key. |
295 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
296 |
def test_add_tracks_size(self): |
297 |
cache = lru_cache.LRUSizeCache() |
|
298 |
self.assertEqual(0, cache._value_size) |
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
299 |
cache['my key'] = 'my value text' |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
300 |
self.assertEqual(13, cache._value_size) |
301 |
||
302 |
def test_remove_tracks_size(self): |
|
303 |
cache = lru_cache.LRUSizeCache() |
|
304 |
self.assertEqual(0, cache._value_size) |
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
305 |
cache['my key'] = 'my value text' |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
306 |
self.assertEqual(13, cache._value_size) |
|
4178.3.3
by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list, |
307 |
node = cache._cache['my key'] |
308 |
cache._remove_node(node) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
309 |
self.assertEqual(0, cache._value_size) |
310 |
||
311 |
def test_no_add_over_size(self): |
|
312 |
"""Adding a large value may not be cached at all.""" |
|
313 |
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5) |
|
314 |
self.assertEqual(0, cache._value_size) |
|
|
6215.1.1
by Martin Packman
Rename confusing LRUCache.items method that doesn't act like dict.items to as_dict |
315 |
self.assertEqual({}, cache.as_dict()) |
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
316 |
cache['test'] = 'key' |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
317 |
self.assertEqual(3, cache._value_size) |
|
6215.1.1
by Martin Packman
Rename confusing LRUCache.items method that doesn't act like dict.items to as_dict |
318 |
self.assertEqual({'test': 'key'}, cache.as_dict()) |
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
319 |
cache['test2'] = 'key that is too big' |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
320 |
self.assertEqual(3, cache._value_size) |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
321 |
self.assertEqual({'test': 'key'}, cache.as_dict()) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
322 |
# If we would add a key, only to cleanup and remove all cached entries,
|
323 |
# then obviously that value should not be stored
|
|
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
324 |
cache['test3'] = 'bigkey' |
325 |
self.assertEqual(3, cache._value_size) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
326 |
self.assertEqual({'test': 'key'}, cache.as_dict()) |
|
6215.1.4
by Martin Packman
Remove unneeded _LRUNode.cleanup callback ability and deprecate LRUCache.add |
327 |
|
328 |
cache['test4'] = 'bikey' |
|
329 |
self.assertEqual(3, cache._value_size) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
330 |
self.assertEqual({'test': 'key'}, cache.as_dict()) |
|
4178.3.7
by John Arbash Meinel
Review tweaks from Ian. |
331 |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
332 |
def test_adding_clears_cache_based_on_size(self): |
333 |
"""The cache is cleared in LRU order until small enough""" |
|
334 |
cache = lru_cache.LRUSizeCache(max_size=20) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
335 |
cache['key1'] = 'value' # 5 chars |
336 |
cache['key2'] = 'value2' # 6 chars |
|
337 |
cache['key3'] = 'value23' # 7 chars |
|
338 |
self.assertEqual(5 + 6 + 7, cache._value_size) |
|
339 |
cache['key2'] # reference key2 so it gets a newer reference time |
|
340 |
cache['key4'] = 'value234' # 8 chars, over limit |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
341 |
# We have to remove 2 keys to get back under limit
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
342 |
self.assertEqual(6 + 8, cache._value_size) |
343 |
self.assertEqual({'key2': 'value2', 'key4': 'value234'}, |
|
|
6215.1.1
by Martin Packman
Rename confusing LRUCache.items method that doesn't act like dict.items to as_dict |
344 |
cache.as_dict()) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
345 |
|
346 |
def test_adding_clears_to_after_cleanup_size(self): |
|
347 |
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
348 |
cache['key1'] = 'value' # 5 chars |
349 |
cache['key2'] = 'value2' # 6 chars |
|
350 |
cache['key3'] = 'value23' # 7 chars |
|
351 |
self.assertEqual(5 + 6 + 7, cache._value_size) |
|
352 |
cache['key2'] # reference key2 so it gets a newer reference time |
|
353 |
cache['key4'] = 'value234' # 8 chars, over limit |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
354 |
# We have to remove 3 keys to get back under limit
|
355 |
self.assertEqual(8, cache._value_size) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
356 |
self.assertEqual({'key4': 'value234'}, cache.as_dict()) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
357 |
|
358 |
def test_custom_sizes(self): |
|
359 |
def size_of_list(lst): |
|
360 |
return sum(len(x) for x in lst) |
|
361 |
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10, |
|
362 |
compute_size=size_of_list) |
|
363 |
||
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
364 |
cache['key1'] = ['val', 'ue'] # 5 chars |
365 |
cache['key2'] = ['val', 'ue2'] # 6 chars |
|
366 |
cache['key3'] = ['val', 'ue23'] # 7 chars |
|
367 |
self.assertEqual(5 + 6 + 7, cache._value_size) |
|
368 |
cache['key2'] # reference key2 so it gets a newer reference time |
|
369 |
cache['key4'] = ['value', '234'] # 8 chars, over limit |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
370 |
# We have to remove 3 keys to get back under limit
|
371 |
self.assertEqual(8, cache._value_size) |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
372 |
self.assertEqual({'key4': ['value', '234']}, cache.as_dict()) |
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
373 |
|
374 |
def test_cleanup(self): |
|
375 |
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10) |
|
376 |
||
377 |
# Add these in order
|
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
378 |
cache['key1'] = 'value' # 5 chars |
379 |
cache['key2'] = 'value2' # 6 chars |
|
380 |
cache['key3'] = 'value23' # 7 chars |
|
381 |
self.assertEqual(5 + 6 + 7, cache._value_size) |
|
|
2993.1.1
by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need |
382 |
|
383 |
cache.cleanup() |
|
384 |
# Only the most recent fits after cleaning up
|
|
385 |
self.assertEqual(7, cache._value_size) |
|
|
3763.8.10
by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache. |
386 |
|
387 |
def test_keys(self): |
|
388 |
cache = lru_cache.LRUSizeCache(max_size=10) |
|
389 |
||
390 |
cache[1] = 'a' |
|
391 |
cache[2] = 'b' |
|
392 |
cache[3] = 'cdef' |
|
393 |
self.assertEqual([1, 2, 3], sorted(cache.keys())) |
|
|
3882.3.1
by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache. |
394 |
|
395 |
def test_resize_smaller(self): |
|
396 |
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9) |
|
397 |
cache[1] = 'abc' |
|
398 |
cache[2] = 'def' |
|
399 |
cache[3] = 'ghi' |
|
400 |
cache[4] = 'jkl' |
|
401 |
# Triggers a cleanup
|
|
402 |
self.assertEqual([2, 3, 4], sorted(cache.keys())) |
|
403 |
# Resize should also cleanup again
|
|
404 |
cache.resize(max_size=6, after_cleanup_size=4) |
|
405 |
self.assertEqual([4], sorted(cache.keys())) |
|
406 |
# Adding should use the new max size
|
|
407 |
cache[5] = 'mno' |
|
408 |
self.assertEqual([4, 5], sorted(cache.keys())) |
|
409 |
cache[6] = 'pqr' |
|
410 |
self.assertEqual([6], sorted(cache.keys())) |
|
411 |
||
412 |
def test_resize_larger(self): |
|
413 |
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9) |
|
414 |
cache[1] = 'abc' |
|
415 |
cache[2] = 'def' |
|
416 |
cache[3] = 'ghi' |
|
417 |
cache[4] = 'jkl' |
|
418 |
# Triggers a cleanup
|
|
419 |
self.assertEqual([2, 3, 4], sorted(cache.keys())) |
|
420 |
cache.resize(max_size=15, after_cleanup_size=12) |
|
421 |
self.assertEqual([2, 3, 4], sorted(cache.keys())) |
|
422 |
cache[5] = 'mno' |
|
423 |
cache[6] = 'pqr' |
|
424 |
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys())) |
|
425 |
cache[7] = 'stu' |
|
426 |
self.assertEqual([4, 5, 6, 7], sorted(cache.keys())) |