Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix LruCache callback deduplication
Browse files Browse the repository at this point in the history
Signed-off-by: Kai A. Hiller <KaiAlexHiller@web.de>
  • Loading branch information
Kai A. Hiller authored and Kai A. Hiller committed Oct 17, 2019
1 parent 6fb0a3d commit a4f062e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.d/6213.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix LruCache callback deduplication.
25 changes: 16 additions & 9 deletions synapse/util/caches/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import inspect
import logging
import threading
from collections import namedtuple
from typing import Any, cast

from six import itervalues
Expand Down Expand Up @@ -625,14 +624,22 @@ def errback(f):
return wrapped


class _CacheContext(namedtuple("_CacheContext", ("cache", "key"))):
# We rely on _CacheContext implementing __eq__ and __hash__ sensibly,
# which namedtuple does for us (i.e. two _CacheContext are the same if
# their caches and keys match). This is important in particular to
# dedupe when we add callbacks to lru cache nodes, otherwise the number
# of callbacks would grow.
def invalidate(self):
self.cache.invalidate(self.key)
class _CacheContext:
# We make sure identical _CacheContext objects share the same invalidate
# function. This is important in particular to dedupe when we add callbacks
# to lru cache nodes, otherwise the number of callbacks would grow.
_invalidate_funcs = {}

def __init__(self, cache, cache_key):
key = (cache, cache_key)

def invalidate():
cache.invalidate(cache_key)

if key not in self._invalidate_funcs:
self._invalidate_funcs[key] = invalidate

self.invalidate = self._invalidate_funcs[key]


def cached(
Expand Down

0 comments on commit a4f062e

Please sign in to comment.