summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Morgan <andrew@amorgan.xyz>2023-03-08 12:21:53 +0000
committerAndrew Morgan <andrew@amorgan.xyz>2023-03-08 12:22:03 +0000
commit2cbf4dd4a4393d66fba993f8ad99377744be8c08 (patch)
tree751975f9971f6e0eb58967227bd20ace58c8f517
parentExtract @cache_in_self decorator to its own utils file (diff)
downloadsynapse-2cbf4dd4a4393d66fba993f8ad99377744be8c08.tar.xz
Modify @cache_in_self decorator to accept self classes other than HomeServer
We want to use this in conjunction with ModuleApi in order to save and
reference singleton instances of module api callback classes.
-rw-r--r--synapse/util/caches/cache_in_self_decorator.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/synapse/util/caches/cache_in_self_decorator.py b/synapse/util/caches/cache_in_self_decorator.py

index fea41b9d08..3b9ded2096 100644 --- a/synapse/util/caches/cache_in_self_decorator.py +++ b/synapse/util/caches/cache_in_self_decorator.py
@@ -1,9 +1,11 @@ -from typing import TYPE_CHECKING, Callable, Dict, List, Optional, TypeVar, cast +import functools +from typing import Callable, TypeVar T = TypeVar("T") +U = TypeVar("U") -def cache_in_self(builder: Callable[["HomeServer"], T]) -> Callable[["HomeServer"], T]: +def cache_in_self(builder: Callable[[T], U]) -> Callable[[T], U]: """Wraps a function called e.g. `get_foo`, checking if `self.foo` exists and returning if so. If not, calls the given function and sets `self.foo` to it. @@ -22,7 +24,7 @@ def cache_in_self(builder: Callable[["HomeServer"], T]) -> Callable[["HomeServer building = [False] @functools.wraps(builder) - def _get(self: "HomeServer") -> T: + def _get(self: T) -> U: try: return getattr(self, depname) except AttributeError: