diff options
author | Paul "LeoNerd" Evans <paul@matrix.org> | 2015-02-23 15:41:54 +0000 |
---|---|---|
committer | Paul "LeoNerd" Evans <paul@matrix.org> | 2015-02-23 15:41:54 +0000 |
commit | e76d485e29498fce7412423e7a5b6ac6bc287ec3 (patch) | |
tree | 75bd25cbe34139c3cfbcc2d9790291e38d5a2d73 /synapse/storage/_base.py | |
parent | Squash out the now-redundant ApplicationServicesCache object class (diff) | |
download | synapse-e76d485e29498fce7412423e7a5b6ac6bc287ec3.tar.xz |
Allow @cached-wrapped functions to have a prefill method for setting entries
Diffstat (limited to 'synapse/storage/_base.py')
-rw-r--r-- | synapse/storage/_base.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py index 78ba5f25ea..4b1ec687c9 100644 --- a/synapse/storage/_base.py +++ b/synapse/storage/_base.py @@ -48,24 +48,30 @@ def cached(max_entries=1000): The wrapped function has an additional member, a callable called "invalidate". This can be used to remove individual entries from the cache. + + The wrapped function has another additional callable, called "prefill", + which can be used to insert values into the cache specifically, without + calling the calculation function. """ def wrap(orig): cache = {} - @defer.inlineCallbacks - def wrapped(self, key): - if key in cache: - defer.returnValue(cache[key]) - - ret = yield orig(self, key) - + def prefill(key, value): while len(cache) > max_entries: # TODO(paul): This feels too biased. However, a random index # would be a bit inefficient, walking the list of keys just # to ignore most of them? del cache[cache.keys()[0]] - cache[key] = ret; + cache[key] = value + + @defer.inlineCallbacks + def wrapped(self, key): + if key in cache: + defer.returnValue(cache[key]) + + ret = yield orig(self, key) + prefill(key, ret) defer.returnValue(ret) def invalidate(key): @@ -73,6 +79,7 @@ def cached(max_entries=1000): del cache[key] wrapped.invalidate = invalidate + wrapped.prefill = prefill return wrapped return wrap |