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

Commit 97647b3

Browse files
authored
Replace DeferredCache with LruCache where possible (#8563)
Most of these uses don't need a full-blown DeferredCache; LruCache is lighter and more appropriate.
1 parent 79c1f97 commit 97647b3

File tree

8 files changed

+30
-27
lines changed

8 files changed

+30
-27
lines changed

changelog.d/8563.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace `DeferredCache` with the lighter-weight `LruCache` where possible.

synapse/replication/slave/storage/client_ips.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from synapse.storage.database import DatabasePool
1717
from synapse.storage.databases.main.client_ips import LAST_SEEN_GRANULARITY
18-
from synapse.util.caches.deferred_cache import DeferredCache
18+
from synapse.util.caches.lrucache import LruCache
1919

2020
from ._base import BaseSlavedStore
2121

@@ -24,9 +24,9 @@ class SlavedClientIpStore(BaseSlavedStore):
2424
def __init__(self, database: DatabasePool, db_conn, hs):
2525
super().__init__(database, db_conn, hs)
2626

27-
self.client_ip_last_seen = DeferredCache(
28-
name="client_ip_last_seen", keylen=4, max_entries=50000
29-
) # type: DeferredCache[tuple, int]
27+
self.client_ip_last_seen = LruCache(
28+
cache_name="client_ip_last_seen", keylen=4, max_size=50000
29+
) # type: LruCache[tuple, int]
3030

3131
async def insert_client_ip(self, user_id, access_token, ip, user_agent, device_id):
3232
now = int(self._clock.time_msec())
@@ -41,7 +41,7 @@ async def insert_client_ip(self, user_id, access_token, ip, user_agent, device_i
4141
if last_seen is not None and (now - last_seen) < LAST_SEEN_GRANULARITY:
4242
return
4343

44-
self.client_ip_last_seen.prefill(key, now)
44+
self.client_ip_last_seen.set(key, now)
4545

4646
self.hs.get_tcp_replication().send_user_ip(
4747
user_id, access_token, ip, user_agent, device_id, now

synapse/storage/_base.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ def _attempt_to_invalidate_cache(
7676
"""
7777

7878
try:
79-
if key is None:
80-
getattr(self, cache_name).invalidate_all()
81-
else:
82-
getattr(self, cache_name).invalidate(tuple(key))
79+
cache = getattr(self, cache_name)
8380
except AttributeError:
8481
# We probably haven't pulled in the cache in this worker,
8582
# which is fine.
86-
pass
83+
return
84+
85+
if key is None:
86+
cache.invalidate_all()
87+
else:
88+
cache.invalidate(tuple(key))
8789

8890

8991
def db_to_json(db_content):

synapse/storage/databases/main/client_ips.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from synapse.metrics.background_process_metrics import wrap_as_background_process
2020
from synapse.storage._base import SQLBaseStore
2121
from synapse.storage.database import DatabasePool, make_tuple_comparison_clause
22-
from synapse.util.caches.deferred_cache import DeferredCache
22+
from synapse.util.caches.lrucache import LruCache
2323

2424
logger = logging.getLogger(__name__)
2525

@@ -410,8 +410,8 @@ def _prune_old_user_ips_txn(txn):
410410
class ClientIpStore(ClientIpWorkerStore):
411411
def __init__(self, database: DatabasePool, db_conn, hs):
412412

413-
self.client_ip_last_seen = DeferredCache(
414-
name="client_ip_last_seen", keylen=4, max_entries=50000
413+
self.client_ip_last_seen = LruCache(
414+
cache_name="client_ip_last_seen", keylen=4, max_size=50000
415415
)
416416

417417
super().__init__(database, db_conn, hs)
@@ -442,7 +442,7 @@ async def insert_client_ip(
442442
if last_seen is not None and (now - last_seen) < LAST_SEEN_GRANULARITY:
443443
return
444444

445-
self.client_ip_last_seen.prefill(key, now)
445+
self.client_ip_last_seen.set(key, now)
446446

447447
self._batch_row_update[key] = (user_agent, device_id, now)
448448

synapse/storage/databases/main/devices.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
)
3535
from synapse.types import Collection, JsonDict, get_verify_key_from_cross_signing_key
3636
from synapse.util import json_decoder, json_encoder
37-
from synapse.util.caches.deferred_cache import DeferredCache
3837
from synapse.util.caches.descriptors import cached, cachedList
38+
from synapse.util.caches.lrucache import LruCache
3939
from synapse.util.iterutils import batch_iter
4040
from synapse.util.stringutils import shortstr
4141

@@ -1005,8 +1005,8 @@ def __init__(self, database: DatabasePool, db_conn, hs):
10051005

10061006
# Map of (user_id, device_id) -> bool. If there is an entry that implies
10071007
# the device exists.
1008-
self.device_id_exists_cache = DeferredCache(
1009-
name="device_id_exists", keylen=2, max_entries=10000
1008+
self.device_id_exists_cache = LruCache(
1009+
cache_name="device_id_exists", keylen=2, max_size=10000
10101010
)
10111011

10121012
async def store_device(
@@ -1052,7 +1052,7 @@ async def store_device(
10521052
)
10531053
if hidden:
10541054
raise StoreError(400, "The device ID is in use", Codes.FORBIDDEN)
1055-
self.device_id_exists_cache.prefill(key, True)
1055+
self.device_id_exists_cache.set(key, True)
10561056
return inserted
10571057
except StoreError:
10581058
raise

synapse/storage/databases/main/events.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,7 @@ def _add_to_cache(self, txn, events_and_contexts):
10511051

10521052
def prefill():
10531053
for cache_entry in to_prefill:
1054-
self.store._get_event_cache.prefill(
1055-
(cache_entry[0].event_id,), cache_entry
1056-
)
1054+
self.store._get_event_cache.set((cache_entry[0].event_id,), cache_entry)
10571055

10581056
txn.call_after(prefill)
10591057

synapse/storage/databases/main/events_worker.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
from synapse.storage.engines import PostgresEngine
4343
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
4444
from synapse.types import Collection, get_domain_from_id
45-
from synapse.util.caches.deferred_cache import DeferredCache
4645
from synapse.util.caches.descriptors import cached
46+
from synapse.util.caches.lrucache import LruCache
4747
from synapse.util.iterutils import batch_iter
4848
from synapse.util.metrics import Measure
4949

@@ -146,11 +146,10 @@ def __init__(self, database: DatabasePool, db_conn, hs):
146146
self._cleanup_old_transaction_ids,
147147
)
148148

149-
self._get_event_cache = DeferredCache(
150-
"*getEvent*",
149+
self._get_event_cache = LruCache(
150+
cache_name="*getEvent*",
151151
keylen=3,
152-
max_entries=hs.config.caches.event_cache_size,
153-
apply_cache_factor_from_config=False,
152+
max_size=hs.config.caches.event_cache_size,
154153
)
155154

156155
self._event_fetch_lock = threading.Condition()
@@ -749,7 +748,7 @@ async def _get_events_from_db(self, event_ids, allow_rejected=False):
749748
event=original_ev, redacted_event=redacted_event
750749
)
751750

752-
self._get_event_cache.prefill((event_id,), cache_entry)
751+
self._get_event_cache.set((event_id,), cache_entry)
753752
result_map[event_id] = cache_entry
754753

755754
return result_map

synapse/util/caches/lrucache.py

+3
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ def cache_contains(key: KT) -> bool:
337337
self.set = cache_set
338338
self.setdefault = cache_set_default
339339
self.pop = cache_pop
340+
# `invalidate` is exposed for consistency with DeferredCache, so that it can be
341+
# invalidated by the cache invalidation replication stream.
342+
self.invalidate = cache_pop
340343
if cache_type is TreeCache:
341344
self.del_multi = cache_del_multi
342345
self.len = synchronized(cache_len)

0 commit comments

Comments
 (0)