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

Commit

Permalink
optimise DeferredCache.set
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Oct 19, 2020
1 parent 7438cbe commit f497ec0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/8593.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minor optimisations in caching code.
15 changes: 12 additions & 3 deletions synapse/util/caches/deferred_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,25 @@ def set(

callbacks = [callback] if callback else []
self.check_thread()
observable = ObservableDeferred(value, consumeErrors=True)
observer = observable.observe()
entry = CacheEntry(deferred=observable, callbacks=callbacks)

existing_entry = self._pending_deferred_cache.pop(key, None)
if existing_entry:
existing_entry.invalidate()

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

# we can save a whole load of effort if the deferred is ready.
if value.called:
self.cache.set(key, value.result, callbacks)
return value

# otherwise, we'll add an entry to the _pending_deferred_cache for now,
# and add callbacks to add it to the cache properly later.

observable = ObservableDeferred(value, consumeErrors=True)
observer = observable.observe()
entry = CacheEntry(deferred=observable, callbacks=callbacks)

self._pending_deferred_cache[key] = entry

def compare_and_pop():
Expand Down

0 comments on commit f497ec0

Please sign in to comment.