summary refs log tree commit diff
path: root/synapse/util/caches/treecache.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-01-29 10:11:21 +0000
committerErik Johnston <erik@matrix.org>2016-01-29 10:11:21 +0000
commit766526e1142e7ad0ffb43bd075b0ff2d6265e4cb (patch)
treea4cd70046cb3cc587cd27192a46132e4f05f3228 /synapse/util/caches/treecache.py
parentReset size on clear (diff)
downloadsynapse-766526e1142e7ad0ffb43bd075b0ff2d6265e4cb.tar.xz
Make TreeCache keep track of its own size.
Diffstat (limited to '')
-rw-r--r--synapse/util/caches/treecache.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/synapse/util/caches/treecache.py b/synapse/util/caches/treecache.py
index 3b58860910..a29ea8144e 100644
--- a/synapse/util/caches/treecache.py
+++ b/synapse/util/caches/treecache.py
@@ -8,6 +8,7 @@ class TreeCache(object):
     Keys must be tuples.
     """
     def __init__(self):
+        self.size = 0
         self.root = {}
 
     def __setitem__(self, key, value):
@@ -21,6 +22,7 @@ class TreeCache(object):
         for k in key[:-1]:
             node = node.setdefault(k, {})
         node[key[-1]] = value
+        self.size += 1
 
     def get(self, key, default=None):
         node = self.root
@@ -31,6 +33,7 @@ class TreeCache(object):
         return node.get(key[-1], default)
 
     def clear(self):
+        self.size = 0
         self.root = {}
 
     def pop(self, key, default=None):
@@ -57,4 +60,8 @@ class TreeCache(object):
                 break
             node_and_keys[i+1][0].pop(k)
 
+        self.size -= 1
         return popped
+
+    def __len__(self):
+        return self.size