Commit d3e8708e authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[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.
parent 9fd7d496e27e
......@@ -155,6 +155,29 @@ class TestQueryCache(TestCase):
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())
......
......@@ -639,6 +639,11 @@ class QueryCache(object):
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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment