Merge pull request #2240 from matrix-org/erikj/cache_list_fix

Update list cache to handle one arg case
pull/2242/head
Erik Johnston 2017-05-22 16:10:46 +01:00 committed by GitHub
commit 33ba90c6e9
1 changed files with 40 additions and 16 deletions

View File

@ -404,6 +404,7 @@ class CacheDescriptor(_CacheDescriptorBase):
wrapped.invalidate_all = cache.invalidate_all wrapped.invalidate_all = cache.invalidate_all
wrapped.cache = cache wrapped.cache = cache
wrapped.num_args = self.num_args
obj.__dict__[self.orig.__name__] = wrapped obj.__dict__[self.orig.__name__] = wrapped
@ -451,8 +452,9 @@ class CacheListDescriptor(_CacheDescriptorBase):
) )
def __get__(self, obj, objtype=None): def __get__(self, obj, objtype=None):
cached_method = getattr(obj, self.cached_method_name)
cache = getattr(obj, self.cached_method_name).cache cache = cached_method.cache
num_args = cached_method.num_args
@functools.wraps(self.orig) @functools.wraps(self.orig)
def wrapped(*args, **kwargs): def wrapped(*args, **kwargs):
@ -469,12 +471,23 @@ class CacheListDescriptor(_CacheDescriptorBase):
results = {} results = {}
cached_defers = {} cached_defers = {}
missing = [] missing = []
for arg in list_args:
key = list(keyargs)
key[self.list_pos] = arg
# If the cache takes a single arg then that is used as the key,
# otherwise a tuple is used.
if num_args == 1:
def cache_get(arg):
return cache.get(arg, callback=invalidate_callback)
else:
key = list(keyargs)
def cache_get(arg):
key[self.list_pos] = arg
return cache.get(tuple(key), callback=invalidate_callback)
for arg in list_args:
try: try:
res = cache.get(tuple(key), callback=invalidate_callback) res = cache_get(arg)
if not isinstance(res, ObservableDeferred): if not isinstance(res, ObservableDeferred):
results[arg] = res results[arg] = res
elif not res.has_succeeded(): elif not res.has_succeeded():
@ -505,17 +518,28 @@ class CacheListDescriptor(_CacheDescriptorBase):
observer = ObservableDeferred(observer) observer = ObservableDeferred(observer)
key = list(keyargs) if num_args == 1:
key[self.list_pos] = arg cache.set(
cache.set( arg, observer,
tuple(key), observer, callback=invalidate_callback
callback=invalidate_callback )
)
def invalidate(f, key): def invalidate(f, key):
cache.invalidate(key) cache.invalidate(key)
return f return f
observer.addErrback(invalidate, tuple(key)) observer.addErrback(invalidate, arg)
else:
key = list(keyargs)
key[self.list_pos] = arg
cache.set(
tuple(key), observer,
callback=invalidate_callback
)
def invalidate(f, key):
cache.invalidate(key)
return f
observer.addErrback(invalidate, tuple(key))
res = observer.observe() res = observer.observe()
res.addCallback(lambda r, arg: (arg, r), arg) res.addCallback(lambda r, arg: (arg, r), arg)