2019-10-18 06:38:59 +00:00
|
|
|
diff --git a/synapse/util/caches/descriptors.py b/synapse/util/caches/descriptors.py
|
2019-11-08 12:00:23 +00:00
|
|
|
index 5ac2530a6..d803d62b8 100644
|
2019-10-18 06:38:59 +00:00
|
|
|
--- a/synapse/util/caches/descriptors.py
|
|
|
|
+++ b/synapse/util/caches/descriptors.py
|
|
|
|
@@ -17,8 +17,8 @@ import functools
|
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
-from collections import namedtuple
|
2019-11-08 12:00:23 +00:00
|
|
|
-from typing import Any, cast
|
|
|
|
+from typing import Any, Tuple, Union, cast
|
2019-10-18 06:38:59 +00:00
|
|
|
+from weakref import WeakValueDictionary
|
|
|
|
|
|
|
|
from six import itervalues
|
|
|
|
|
2019-11-08 12:00:23 +00:00
|
|
|
@@ -38,6 +38,8 @@ from . import register_cache
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
+CacheKey = Union[Tuple, Any]
|
|
|
|
+
|
|
|
|
|
|
|
|
class _CachedFunction(Protocol):
|
|
|
|
invalidate = None # type: Any
|
|
|
|
@@ -430,7 +432,7 @@ class CacheDescriptor(_CacheDescriptorBase):
|
2019-10-18 06:38:59 +00:00
|
|
|
# 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)
|
|
|
|
+ kwargs["cache_context"] = _CacheContext.get_instance(cache, cache_key)
|
|
|
|
|
|
|
|
try:
|
|
|
|
cached_result_d = cache.get(cache_key, callback=invalidate_callback)
|
2019-11-08 12:00:23 +00:00
|
|
|
@@ -625,14 +627,38 @@ class CacheListDescriptor(_CacheDescriptorBase):
|
2019-10-18 06:38:59 +00:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
-class _CacheContext(namedtuple("_CacheContext", ("cache", "key"))):
|
|
|
|
- # We rely on _CacheContext implementing __eq__ and __hash__ sensibly,
|
|
|
|
- # which namedtuple does for us (i.e. two _CacheContext are the same if
|
|
|
|
- # their caches and keys match). This is important in particular to
|
|
|
|
- # dedupe when we add callbacks to lru cache nodes, otherwise the number
|
|
|
|
- # of callbacks would grow.
|
2019-11-08 12:00:23 +00:00
|
|
|
- def invalidate(self):
|
|
|
|
- self.cache.invalidate(self.key)
|
2019-10-18 06:38:59 +00:00
|
|
|
+class _CacheContext:
|
|
|
|
+ """Holds cache information from the cached function higher in the calling order.
|
|
|
|
+
|
|
|
|
+ Can be used to invalidate the higher level cache entry if something changes
|
|
|
|
+ on a lower level.
|
|
|
|
+ """
|
|
|
|
+
|
2019-11-08 12:00:23 +00:00
|
|
|
+ _cache_context_objects = (
|
|
|
|
+ WeakValueDictionary()
|
|
|
|
+ ) # type: WeakValueDictionary[Tuple[Cache, CacheKey], _CacheContext]
|
2019-10-18 06:38:59 +00:00
|
|
|
+
|
2019-11-08 12:00:23 +00:00
|
|
|
+ def __init__(self, cache, cache_key): # type: (Cache, CacheKey) -> None
|
2019-10-18 06:38:59 +00:00
|
|
|
+ self._cache = cache
|
|
|
|
+ self._cache_key = cache_key
|
|
|
|
+
|
2019-11-08 12:00:23 +00:00
|
|
|
+ def invalidate(self): # type: () -> None
|
2019-10-18 06:38:59 +00:00
|
|
|
+ """Invalidates the cache entry referred to by the context."""
|
|
|
|
+ self._cache.invalidate(self._cache_key)
|
|
|
|
+
|
|
|
|
+ @classmethod
|
2019-11-08 12:00:23 +00:00
|
|
|
+ def get_instance(cls, cache, cache_key): # type: (Cache, CacheKey) -> _CacheContext
|
2019-10-18 06:38:59 +00:00
|
|
|
+ """Returns an instance constructed with the given arguments.
|
|
|
|
+
|
|
|
|
+ A new instance is only created if none already exists.
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ # We make sure there are no identical _CacheContext instances. This is
|
|
|
|
+ # important in particular to dedupe when we add callbacks to lru cache
|
|
|
|
+ # nodes, otherwise the number of callbacks would grow.
|
|
|
|
+ return cls._cache_context_objects.setdefault(
|
|
|
|
+ (cache, cache_key), cls(cache, cache_key)
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
|
|
def cached(
|