Rev 3773: Add a .keys() member to LRUCache and LRUSizeCache. in http://bzr.arbash-meinel.com/branches/bzr/1.8-dev/btree_buffer
John Arbash Meinel
john at arbash-meinel.com
Tue Oct 14 21:19:26 BST 2008
At http://bzr.arbash-meinel.com/branches/bzr/1.8-dev/btree_buffer
------------------------------------------------------------
revno: 3773
revision-id: john at arbash-meinel.com-20081014201906-5rtgq3yvzent0gwe
parent: john at arbash-meinel.com-20081014193134-yi1otoetaq96obxf
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: btree_buffer
timestamp: Tue 2008-10-14 15:19:06 -0500
message:
Add a .keys() member to LRUCache and LRUSizeCache.
-------------- next part --------------
=== modified file 'bzrlib/lru_cache.py'
--- a/bzrlib/lru_cache.py 2008-04-24 07:22:53 +0000
+++ b/bzrlib/lru_cache.py 2008-10-14 20:19:06 +0000
@@ -74,6 +74,17 @@
return self[key]
return default
+ def keys(self):
+ """Get the list of keys currently cached.
+
+ Note that values returned here may not be available by the time you
+ request them later. This is simply meant as a peak into the current
+ state.
+
+ :return: An unordered list of keys that are currently cached.
+ """
+ return self._cache.keys()
+
def cleanup(self):
"""Clear the cache until it shrinks to the requested size.
=== modified file 'bzrlib/tests/test_lru_cache.py'
--- a/bzrlib/tests/test_lru_cache.py 2007-11-16 23:53:17 +0000
+++ b/bzrlib/tests/test_lru_cache.py 2008-10-14 20:19:06 +0000
@@ -213,6 +213,18 @@
obj = object()
self.assertIs(obj, cache.get(3, obj))
+ def test_keys(self):
+ cache = lru_cache.LRUCache(max_cache=5)
+
+ cache[1] = 2
+ cache[2] = 3
+ cache[3] = 4
+ self.assertEqual([1, 2, 3], sorted(cache.keys()))
+ cache[4] = 5
+ cache[5] = 6
+ cache[6] = 7
+ self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
+
class TestLRUSizeCache(tests.TestCase):
@@ -312,3 +324,11 @@
cache.cleanup()
# Only the most recent fits after cleaning up
self.assertEqual(7, cache._value_size)
+
+ def test_keys(self):
+ cache = lru_cache.LRUSizeCache(max_size=10)
+
+ cache[1] = 'a'
+ cache[2] = 'b'
+ cache[3] = 'cdef'
+ self.assertEqual([1, 2, 3], sorted(cache.keys()))
More information about the bazaar-commits
mailing list