This repository was archived by the owner on Apr 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change 1+ Minor optimisations in caching code.
Original file line number Diff line number Diff 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 ():
You can’t perform that action at this time.
0 commit comments