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

Optimise async get event lookups #13435

Merged
merged 11 commits into from
Aug 4, 2022
30 changes: 27 additions & 3 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,31 @@ def _invalidate_local_get_event_cache(self, event_id: str) -> None:
async def _get_events_from_cache(
self, events: Iterable[str], update_metrics: bool = True
) -> Dict[str, EventCacheEntry]:
"""Fetch events from the caches.
"""Fetch events from the caches, both in memory and any external.

May return rejected events.

Args:
events: list of event_ids to fetch
update_metrics: Whether to update the cache hit ratio metrics
"""
event_map = self._get_events_from_local_cache(events, update_metrics=update_metrics)
richvdh marked this conversation as resolved.
Show resolved Hide resolved

missing_event_ids = {e for e in events if e not in event_map}
Fizzadar marked this conversation as resolved.
Show resolved Hide resolved

for event_id in missing_event_ids:
ret = await self._get_event_cache.get(
(event_id,), None, update_metrics=update_metrics
)
richvdh marked this conversation as resolved.
Show resolved Hide resolved
if ret:
event_map[event_id] = ret

return event_map

def _get_events_from_local_cache(
self, events: Iterable[str], update_metrics: bool = True
) -> Dict[str, EventCacheEntry]:
"""Fetch events from the local, in memory, caches.

May return rejected events.

Expand All @@ -766,7 +790,7 @@ async def _get_events_from_cache(

for event_id in events:
# First check if it's in the event cache
ret = await self._get_event_cache.get(
ret = self._get_event_cache.get_local(
(event_id,), None, update_metrics=update_metrics
)
if ret:
Expand All @@ -788,7 +812,7 @@ async def _get_events_from_cache(

# We add the entry back into the cache as we want to keep
# recently queried events in the cache.
await self._get_event_cache.set((event_id,), cache_entry)
self._get_event_cache.set_local((event_id,), cache_entry)

return event_map

Expand Down
8 changes: 8 additions & 0 deletions synapse/util/caches/lrucache.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,17 @@ async def get(
) -> Optional[VT]:
return self._lru_cache.get(key, update_metrics=update_metrics)

def get_local(
self, key: KT, default: Optional[T] = None, update_metrics: bool = True
) -> Optional[VT]:
return self._lru_cache.get(key, update_metrics=update_metrics)

async def set(self, key: KT, value: VT) -> None:
self._lru_cache.set(key, value)

def set_local(self, key: KT, value: VT) -> None:
self._lru_cache.set(key, value)

async def invalidate(self, key: KT) -> None:
# This method should invalidate any external cache and then invalidate the LruCache.
return self._lru_cache.invalidate(key)
Expand Down