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

Commit f497ec0

Browse files
committed
optimise DeferredCache.set
1 parent 7438cbe commit f497ec0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

changelog.d/8593.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Minor optimisations in caching code.

synapse/util/caches/deferred_cache.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,25 @@ def set(
214214

215215
callbacks = [callback] if callback else []
216216
self.check_thread()
217-
observable = ObservableDeferred(value, consumeErrors=True)
218-
observer = observable.observe()
219-
entry = CacheEntry(deferred=observable, callbacks=callbacks)
220217

221218
existing_entry = self._pending_deferred_cache.pop(key, None)
222219
if existing_entry:
223220
existing_entry.invalidate()
224221

225222
# XXX: why don't we invalidate the entry in `self.cache` yet?
226223

224+
# we can save a whole load of effort if the deferred is ready.
225+
if value.called:
226+
self.cache.set(key, value.result, callbacks)
227+
return value
228+
229+
# otherwise, we'll add an entry to the _pending_deferred_cache for now,
230+
# and add callbacks to add it to the cache properly later.
231+
232+
observable = ObservableDeferred(value, consumeErrors=True)
233+
observer = observable.observe()
234+
entry = CacheEntry(deferred=observable, callbacks=callbacks)
235+
227236
self._pending_deferred_cache[key] = entry
228237

229238
def compare_and_pop():

0 commit comments

Comments
 (0)