mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-25 20:29:23 -05:00
Merge branch 'erikj/cache_perf' of github.com:matrix-org/synapse into develop
This commit is contained in:
commit
3b096c5f5c
@ -102,6 +102,15 @@ class ObservableDeferred(object):
|
|||||||
def observers(self):
|
def observers(self):
|
||||||
return self._observers
|
return self._observers
|
||||||
|
|
||||||
|
def has_called(self):
|
||||||
|
return self._result is not None
|
||||||
|
|
||||||
|
def has_succeeded(self):
|
||||||
|
return self._result is not None and self._result[0] is True
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
return self._result[1]
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return getattr(self._deferred, name)
|
return getattr(self._deferred, name)
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import functools
|
|||||||
import inspect
|
import inspect
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -302,16 +303,21 @@ class CacheListDescriptor(object):
|
|||||||
|
|
||||||
# cached is a dict arg -> deferred, where deferred results in a
|
# cached is a dict arg -> deferred, where deferred results in a
|
||||||
# 2-tuple (`arg`, `result`)
|
# 2-tuple (`arg`, `result`)
|
||||||
cached = {}
|
results = {}
|
||||||
|
cached_defers = {}
|
||||||
missing = []
|
missing = []
|
||||||
for arg in list_args:
|
for arg in list_args:
|
||||||
key = list(keyargs)
|
key = list(keyargs)
|
||||||
key[self.list_pos] = arg
|
key[self.list_pos] = arg
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = cache.get(tuple(key)).observe()
|
res = cache.get(tuple(key))
|
||||||
res.addCallback(lambda r, arg: (arg, r), arg)
|
if not res.has_succeeded():
|
||||||
cached[arg] = res
|
res = res.observe()
|
||||||
|
res.addCallback(lambda r, arg: (arg, r), arg)
|
||||||
|
cached_defers[arg] = res
|
||||||
|
else:
|
||||||
|
results[arg] = res.get_result()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
missing.append(arg)
|
missing.append(arg)
|
||||||
|
|
||||||
@ -349,12 +355,21 @@ class CacheListDescriptor(object):
|
|||||||
res = observer.observe()
|
res = observer.observe()
|
||||||
res.addCallback(lambda r, arg: (arg, r), arg)
|
res.addCallback(lambda r, arg: (arg, r), arg)
|
||||||
|
|
||||||
cached[arg] = res
|
cached_defers[arg] = res
|
||||||
|
|
||||||
return preserve_context_over_deferred(defer.gatherResults(
|
if cached_defers:
|
||||||
cached.values(),
|
def update_results_dict(res):
|
||||||
consumeErrors=True,
|
results.update(res)
|
||||||
).addErrback(unwrapFirstError).addCallback(lambda res: dict(res)))
|
return results
|
||||||
|
|
||||||
|
return preserve_context_over_deferred(defer.gatherResults(
|
||||||
|
cached_defers.values(),
|
||||||
|
consumeErrors=True,
|
||||||
|
).addCallback(update_results_dict).addErrback(
|
||||||
|
unwrapFirstError
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
return results
|
||||||
|
|
||||||
obj.__dict__[self.orig.__name__] = wrapped
|
obj.__dict__[self.orig.__name__] = wrapped
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user