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

Commit

Permalink
Make LruCacheget_multi return something sane.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jul 19, 2022
1 parent d4133b2 commit 88aa56c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions synapse/util/caches/dictionary_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing_extensions import Literal

from synapse.util.caches.lrucache import LruCache
from synapse.util.caches.treecache import TreeCache, iterate_tree_cache_items
from synapse.util.caches.treecache import TreeCache

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -228,9 +228,9 @@ def _get_full_dict(
# and `_PerKeyValue` into the `DictionaryEntry`.
values = {}
known_absent = set()
for key_tuple, node in iterate_tree_cache_items((), all_entries):
dict_key = key_tuple[0]
dict_value = node.value
for cache_key, dict_value in all_entries:
# The key used for the `TreeCache` is `(key, dict_key)`
dict_key = cache_key[1]

# We have explicitly looked for a full cache key, so we
# shouldn't see one.
Expand Down
27 changes: 21 additions & 6 deletions synapse/util/caches/lrucache.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
Callable,
Collection,
Dict,
Generator,
Generic,
List,
Optional,
Tuple,
Type,
TypeVar,
Union,
Expand All @@ -46,8 +48,8 @@
from synapse.util.caches import CacheMetric, EvictionReason, register_cache
from synapse.util.caches.treecache import (
TreeCache,
TreeCacheNode,
iterate_tree_cache_entry,
iterate_tree_cache_items,
)
from synapse.util.linked_list import ListNode

Expand Down Expand Up @@ -596,32 +598,45 @@ def cache_get_multi(
key: tuple,
default: Literal[None] = None,
update_metrics: bool = True,
) -> Union[None, TreeCacheNode]:
) -> Union[None, Generator[Tuple[KT, VT], None, None]]:
...

@overload
def cache_get_multi(
key: tuple,
default: T,
update_metrics: bool = True,
) -> Union[T, TreeCacheNode]:
) -> Union[T, Generator[Tuple[KT, VT], None, None]]:
...

@synchronized
def cache_get_multi(
key: tuple,
default: Optional[T] = None,
update_metrics: bool = True,
) -> Union[None, T, TreeCacheNode]:
"""Used only for `TreeCache` to fetch a subtree."""
) -> Union[None, T, Generator[Tuple[KT, VT], None, None]]:
"""Returns a generator yielding all entries under the given key.
Can only be used if backed by a tree cache.
Returns:
Either default if the key doesn't exist, or a generator of the
key/value pairs.
"""

assert isinstance(cache, TreeCache)

node = cache.get(key, None)
if node is not None:
if update_metrics and metrics:
metrics.inc_hits()
return node

# Iterating over the node will return values of type `_Node`,
# which we need to unwrap.
return (
(full_key, lru_node.value)
for full_key, lru_node in iterate_tree_cache_items(key, node)
)
else:
if update_metrics and metrics:
metrics.inc_misses()
Expand Down

0 comments on commit 88aa56c

Please sign in to comment.