Rev 2999: Implement LRUCache.get() which acts like dict.get() in http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/push_packs_to_knits

John Arbash Meinel john at arbash-meinel.com
Fri Nov 16 23:53:56 GMT 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/push_packs_to_knits

------------------------------------------------------------
revno: 2999
revision-id:john at arbash-meinel.com-20071116235317-uymqhilped1rloqy
parent: pqm at pqm.ubuntu.com-20071115144759-zx0nd44rgp38riwr
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: push_packs_to_knits
timestamp: Fri 2007-11-16 17:53:17 -0600
message:
  Implement LRUCache.get() which acts like dict.get()
  so that we can return a default if the key isn't present.
modified:
  bzrlib/lru_cache.py            lru_cache.py-20070119165515-tlw203kuwh0id5gv-1
  bzrlib/tests/test_lru_cache.py test_lru_cache.py-20070119165535-hph6rk4h9rzy4180-1
-------------- next part --------------
=== modified file 'bzrlib/lru_cache.py'
--- a/bzrlib/lru_cache.py	2007-11-14 21:07:54 +0000
+++ b/bzrlib/lru_cache.py	2007-11-16 23:53:17 +0000
@@ -69,6 +69,11 @@
             # Trigger the cleanup
             self.cleanup()
 
+    def get(self, key, default=None):
+        if key in self._cache:
+            return self[key]
+        return default
+
     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-14 21:07:54 +0000
+++ b/bzrlib/tests/test_lru_cache.py	2007-11-16 23:53:17 +0000
@@ -203,6 +203,16 @@
         self.assertEqual([1, 4, 5, 3, 2], list(cache._queue))
         self.assertEqual({1:1, 2:1, 3:1, 4:1, 5:1}, cache._refcount)
 
+    def test_get(self):
+        cache = lru_cache.LRUCache(max_cache=5)
+
+        cache.add(1, 10)
+        cache.add(2, 20)
+        self.assertEqual(20, cache.get(2))
+        self.assertIs(None, cache.get(3))
+        obj = object()
+        self.assertIs(obj, cache.get(3, obj))
+
 
 class TestLRUSizeCache(tests.TestCase):
 



More information about the bazaar-commits mailing list