summary refs log tree commit diff
path: root/synapse/util/caches/descriptors.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-08-19 15:58:52 +0100
committerErik Johnston <erik@matrix.org>2016-08-19 16:09:16 +0100
commit45fd2c8942009a634cf38a90ca1f306aae7022fc (patch)
treea37d4431236537d669209c10281e3d798fcc21fa /synapse/util/caches/descriptors.py
parentRename to on_invalidate (diff)
downloadsynapse-45fd2c8942009a634cf38a90ca1f306aae7022fc.tar.xz
Ensure invalidation list does not grow unboundedly
Diffstat (limited to 'synapse/util/caches/descriptors.py')
-rw-r--r--synapse/util/caches/descriptors.py20
1 files changed, 8 insertions, 12 deletions
diff --git a/synapse/util/caches/descriptors.py b/synapse/util/caches/descriptors.py
index e93ff40dc0..8dba61d49f 100644
--- a/synapse/util/caches/descriptors.py
+++ b/synapse/util/caches/descriptors.py
@@ -25,6 +25,7 @@ from synapse.util.logcontext import (
 from . import DEBUG_CACHES, register_cache
 
 from twisted.internet import defer
+from collections import namedtuple
 
 import os
 import functools
@@ -210,16 +211,17 @@ class CacheDescriptor(object):
             # whenever we are invalidated
             invalidate_callback = kwargs.pop("on_invalidate", None)
 
-            # Add our own `cache_context` to argument list if the wrapped function
-            # has asked for one
-            self_context = _CacheContext(cache, None)
+            # Add temp cache_context so inspect.getcallargs doesn't explode
             if self.add_cache_context:
-                kwargs["cache_context"] = self_context
+                kwargs["cache_context"] = None
 
             arg_dict = inspect.getcallargs(self.orig, obj, *args, **kwargs)
             cache_key = tuple(arg_dict[arg_nm] for arg_nm in self.arg_names)
 
-            self_context.key = cache_key
+            # Add our own `cache_context` to argument list if the wrapped function
+            # has asked for one
+            if self.add_cache_context:
+                kwargs["cache_context"] = _CacheContext(cache, cache_key)
 
             try:
                 cached_result_d = cache.get(cache_key, callback=invalidate_callback)
@@ -414,13 +416,7 @@ class CacheListDescriptor(object):
         return wrapped
 
 
-class _CacheContext(object):
-    __slots__ = ["cache", "key"]
-
-    def __init__(self, cache, key):
-        self.cache = cache
-        self.key = key
-
+class _CacheContext(namedtuple("_CacheContext", ("cache", "key"))):
     def invalidate(self):
         self.cache.invalidate(self.key)