Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 759f9c0

Browse files
authored
Fix caching behavior for relations push rules. (#12859)
By always returning all requested values from the function wrapped by cachedList. Otherwise implicit None values get added into the cache, which are unexpected.
1 parent 4cbcd4a commit 759f9c0

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

changelog.d/12859.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Experimental support for [MSC3772](https://github.com/matrix-org/matrix-spec-proposals/pull/3772): Push rule for mutually related events.

synapse/storage/databases/main/relations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import logging
16-
from collections import defaultdict
1716
from typing import (
1817
Collection,
1918
Dict,
@@ -810,7 +809,9 @@ def _get_event_relations(
810809
txn: LoggingTransaction,
811810
) -> Dict[str, Set[Tuple[str, str]]]:
812811
txn.execute(sql, [event_id] + rel_type_args)
813-
result = defaultdict(set)
812+
result: Dict[str, Set[Tuple[str, str]]] = {
813+
rel_type: set() for rel_type in relation_types
814+
}
814815
for rel_type, sender, type in txn.fetchall():
815816
result[rel_type].add((sender, type))
816817
return result

synapse/util/caches/descriptors.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,14 @@ def cached(
595595
def cachedList(
596596
*, cached_method_name: str, list_name: str, num_args: Optional[int] = None
597597
) -> Callable[[F], _CachedFunction[F]]:
598-
"""Creates a descriptor that wraps a function in a `CacheListDescriptor`.
598+
"""Creates a descriptor that wraps a function in a `DeferredCacheListDescriptor`.
599599
600-
Used to do batch lookups for an already created cache. A single argument
600+
Used to do batch lookups for an already created cache. One of the arguments
601601
is specified as a list that is iterated through to lookup keys in the
602602
original cache. A new tuple consisting of the (deduplicated) keys that weren't in
603-
the cache gets passed to the original function, the result of which is stored in the
604-
cache.
603+
the cache gets passed to the original function, which is expected to results
604+
in a map of key to value for each passed value. THe new results are stored in the
605+
original cache. Note that any missing values are cached as None.
605606
606607
Args:
607608
cached_method_name: The name of the single-item lookup method.
@@ -614,11 +615,11 @@ def cachedList(
614615
Example:
615616
616617
class Example:
617-
@cached(num_args=2)
618-
def do_something(self, first_arg):
618+
@cached()
619+
def do_something(self, first_arg, second_arg):
619620
...
620621
621-
@cachedList(do_something.cache, list_name="second_args", num_args=2)
622+
@cachedList(cached_method_name="do_something", list_name="second_args")
622623
def batch_do_something(self, first_arg, second_args):
623624
...
624625
"""

0 commit comments

Comments
 (0)