diff options
author | Richard van der Hoff <github@rvanderhoff.org.uk> | 2018-02-05 16:34:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-05 16:34:36 +0100 |
commit | db91e72adee90f7d7bacf25f0ed517074c46e64a (patch) | |
tree | e6e8d43cd2c17bb056fb798c059d72451dd210a8 /synapse/util/caches/lrucache.py | |
parent | Fix broken unit test for media storage (diff) | |
parent | report metrics on number of cache evictions (diff) | |
download | synapse-db91e72adee90f7d7bacf25f0ed517074c46e64a.tar.xz |
Merge pull request #2844 from matrix-org/rav/evicted_metrics
montoring metrics for number of cache evictions
Diffstat (limited to 'synapse/util/caches/lrucache.py')
-rw-r--r-- | synapse/util/caches/lrucache.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/synapse/util/caches/lrucache.py b/synapse/util/caches/lrucache.py index cf5fbb679c..f088dd430e 100644 --- a/synapse/util/caches/lrucache.py +++ b/synapse/util/caches/lrucache.py @@ -49,7 +49,24 @@ class LruCache(object): Can also set callbacks on objects when getting/setting which are fired when that key gets invalidated/evicted. """ - def __init__(self, max_size, keylen=1, cache_type=dict, size_callback=None): + def __init__(self, max_size, keylen=1, cache_type=dict, size_callback=None, + evicted_callback=None): + """ + Args: + max_size (int): + + keylen (int): + + cache_type (type): + type of underlying cache to be used. Typically one of dict + or TreeCache. + + size_callback (func(V) -> int | None): + + evicted_callback (func(int)|None): + if not None, called on eviction with the size of the evicted + entry + """ cache = cache_type() self.cache = cache # Used for introspection. list_root = _Node(None, None, None, None) @@ -61,8 +78,10 @@ class LruCache(object): def evict(): while cache_len() > max_size: todelete = list_root.prev_node - delete_node(todelete) + evicted_len = delete_node(todelete) cache.pop(todelete.key, None) + if evicted_callback: + evicted_callback(evicted_len) def synchronized(f): @wraps(f) @@ -111,12 +130,15 @@ class LruCache(object): prev_node.next_node = next_node next_node.prev_node = prev_node + deleted_len = 1 if size_callback: - cached_cache_len[0] -= size_callback(node.value) + deleted_len = size_callback(node.value) + cached_cache_len[0] -= deleted_len for cb in node.callbacks: cb() node.callbacks.clear() + return deleted_len @synchronized def cache_get(key, default=None, callbacks=[]): |