# HG changeset patch # User Laurent Wouters # Date 1524583004 -7200 # Node ID c96dd92e480e3625d18a28b1e71fe147f7f6ae4b # Parent 9fd7d496e27efccc570cafe35c5702354e271246 [utils] Implements __iter__ on QueryCache In order to be able to iterate over the currently cached queries (so that they may be inspected and invalidated), this changeset implementes __iter__ on QueryCache. diff -r 9fd7d496e27e -r c96dd92e480e cubicweb/test/unittest_utils.py --- a/cubicweb/test/unittest_utils.py Tue Apr 24 17:30:43 2018 +0200 +++ b/cubicweb/test/unittest_utils.py Tue Apr 24 17:16:44 2018 +0200 @@ -155,6 +155,29 @@ self.assertEqual(v, -1) + def test_iterator(self): + """ + Tests the iterating on key-value couples in the cache + """ + c = QueryCache(ceiling=20) + # set 10 values + for x in range(10): + c[x] = x + # arrange for the first 5 to be permanent + for x in range(5): + for r in range(QueryCache._maxlevel + 2): + v = c[x] + self.assertEqual(v, x) + self.assertEqual(c._usage_report(), + {'transientcount': 0, + 'itemcount': 10, + 'permanentcount': 5}) + content = sorted(c) + for x in range(10): + self.assertEquals(x, content[x][0]) + self.assertEquals(x, content[x][1]) + + class UStringIOTC(TestCase): def test_boolean_value(self): self.assertTrue(UStringIO()) diff -r 9fd7d496e27e -r c96dd92e480e cubicweb/utils.py --- a/cubicweb/utils.py Tue Apr 24 17:30:43 2018 +0200 +++ b/cubicweb/utils.py Tue Apr 24 17:16:44 2018 +0200 @@ -639,6 +639,11 @@ except KeyError: return default + def __iter__(self): + with self._lock: + for k, v in self._data.items(): + yield k, v + def __getitem__(self, k): with self._lock: if k in self._permanent: