Allow creating an LruCache that's not affected by cache factor

pull/6391/head
Andrew Morgan 2020-05-01 15:35:46 +01:00
parent fa56e16c85
commit 83cf583adf
4 changed files with 26 additions and 4 deletions

View File

@ -75,7 +75,10 @@ class EventsWorkerStore(SQLBaseStore):
super(EventsWorkerStore, self).__init__(database, db_conn, hs)
self._get_event_cache = Cache(
"*getEvent*", keylen=3, max_entries=hs.config.caches.event_cache_size
"*getEvent*",
keylen=3,
max_entries=hs.config.caches.event_cache_size,
apply_cache_factor_from_config=False,
)
self._event_fetch_lock = threading.Condition()

View File

@ -88,7 +88,15 @@ class Cache(object):
"_pending_deferred_cache",
)
def __init__(self, name, max_entries=1000, keylen=1, tree=False, iterable=False):
def __init__(
self,
name,
max_entries=1000,
keylen=1,
tree=False,
iterable=False,
apply_cache_factor_from_config=True,
):
cache_type = TreeCache if tree else dict
self._pending_deferred_cache = cache_type()
@ -98,6 +106,7 @@ class Cache(object):
cache_type=cache_type,
size_callback=(lambda d: len(d)) if iterable else None,
evicted_callback=self._on_evicted,
apply_cache_factor_from_config=apply_cache_factor_from_config,
)
self.name = name

View File

@ -52,11 +52,11 @@ class ExpiringCache(object):
an item on access. Defaults to False.
iterable (bool): If true, the size is calculated by summing the
sizes of all entries, rather than the number of entries.
"""
self._cache_name = cache_name
self._original_max_size = max_len
self._max_size = int(max_len * cache_config.properties.default_factor_size)
self._clock = clock

View File

@ -57,6 +57,7 @@ class LruCache(object):
cache_type=dict,
size_callback=None,
evicted_callback=None,
apply_cache_factor_from_config=True,
):
"""
Args:
@ -73,13 +74,22 @@ class LruCache(object):
evicted_callback (func(int)|None):
if not None, called on eviction with the size of the evicted
entry
apply_cache_factor_from_config (bool): If true, `max_size` will be
multiplied by a cache factor derived from the homeserver config.
"""
cache = cache_type()
self.cache = cache # Used for introspection.
# Save the original max size, and apply the default size factor.
self._original_max_size = max_size
self.max_size = int(max_size * cache_config.properties.default_factor_size)
# We previously didn't apply the cache factor here, and as such some caches were
# not affected by the global cache factor. Add an option here to disable applying
# the cache factor when a cache is created
if apply_cache_factor_from_config:
self.max_size = int(max_size * cache_config.properties.default_factor_size)
else:
self.max_size = int(max_size)
list_root = _Node(None, None, None, None)
list_root.next_node = list_root