mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-10-01 08:25:44 -04:00
optimise DeferredCache.set
This commit is contained in:
parent
9146a8a691
commit
2b3af01791
1
changelog.d/8593.misc
Normal file
1
changelog.d/8593.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Minor optimisations in caching code.
|
@ -214,9 +214,6 @@ class DeferredCache(Generic[KT, VT]):
|
|||||||
|
|
||||||
callbacks = [callback] if callback else []
|
callbacks = [callback] if callback else []
|
||||||
self.check_thread()
|
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)
|
existing_entry = self._pending_deferred_cache.pop(key, None)
|
||||||
if existing_entry:
|
if existing_entry:
|
||||||
@ -224,6 +221,18 @@ class DeferredCache(Generic[KT, VT]):
|
|||||||
|
|
||||||
# XXX: why don't we invalidate the entry in `self.cache` yet?
|
# 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
|
self._pending_deferred_cache[key] = entry
|
||||||
|
|
||||||
def compare_and_pop():
|
def compare_and_pop():
|
||||||
|
Loading…
Reference in New Issue
Block a user