Environment takes precedence over config values
parent
424215a0c3
commit
c7f3bf66c2
|
@ -103,17 +103,11 @@ class CacheConfig(Config):
|
||||||
# Set the global one so that it's reflected in new caches
|
# Set the global one so that it's reflected in new caches
|
||||||
properties.default_factor_size = self.global_factor
|
properties.default_factor_size = self.global_factor
|
||||||
|
|
||||||
# Load cache factors from the environment, but override them with the
|
# Load cache factors from the config, but override them with the
|
||||||
# ones in the config file if they exist
|
# environment if they exist
|
||||||
individual_factors = {
|
individual_factors = cache_config.get("per_cache_factors", {}) or {}
|
||||||
key[len(_CACHE_PREFIX) + 1 :].lower(): float(val)
|
if not isinstance(individual_factors, dict):
|
||||||
for key, val in self._environ.items()
|
|
||||||
if key.startswith(_CACHE_PREFIX + "_")
|
|
||||||
}
|
|
||||||
individual_factors_config = cache_config.get("per_cache_factors", {}) or {}
|
|
||||||
if not isinstance(individual_factors_config, dict):
|
|
||||||
raise ConfigError("caches.per_cache_factors must be a dictionary")
|
raise ConfigError("caches.per_cache_factors must be a dictionary")
|
||||||
individual_factors.update(individual_factors_config)
|
|
||||||
|
|
||||||
for cache, factor in individual_factors.items():
|
for cache, factor in individual_factors.items():
|
||||||
if not isinstance(factor, (int, float)):
|
if not isinstance(factor, (int, float)):
|
||||||
|
@ -122,6 +116,15 @@ class CacheConfig(Config):
|
||||||
)
|
)
|
||||||
self.cache_factors[cache.lower()] = factor
|
self.cache_factors[cache.lower()] = factor
|
||||||
|
|
||||||
|
# Override with environment
|
||||||
|
individual_factors.update(
|
||||||
|
{
|
||||||
|
key[len(_CACHE_PREFIX) + 1 :].lower(): float(val)
|
||||||
|
for key, val in self._environ.items()
|
||||||
|
if key.startswith(_CACHE_PREFIX + "_")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# Resize all caches (if necessary) with the new factors we've loaded
|
# Resize all caches (if necessary) with the new factors we've loaded
|
||||||
self.resize_all_caches()
|
self.resize_all_caches()
|
||||||
|
|
||||||
|
|
|
@ -90,13 +90,27 @@ class Cache(object):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name,
|
name: str,
|
||||||
max_entries=1000,
|
max_entries: int = 1000,
|
||||||
keylen=1,
|
keylen: int = 1,
|
||||||
tree=False,
|
tree: bool = False,
|
||||||
iterable=False,
|
iterable: bool = False,
|
||||||
apply_cache_factor_from_config=True,
|
apply_cache_factor_from_config: bool = True,
|
||||||
):
|
):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
name: The name of the cache
|
||||||
|
max_entries: Maximum amount of entries that the cache will hold
|
||||||
|
keylen: The length of the tuple used as the cache key
|
||||||
|
tree: Use a TreeCache instead of a dict as the underlying cache type
|
||||||
|
iterable: If True, count each item in the cached object as an entry,
|
||||||
|
rather than each cached object
|
||||||
|
apply_cache_factor_from_config: Whether cache factors specified in the
|
||||||
|
config file affect `max_entries`
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Cache
|
||||||
|
"""
|
||||||
cache_type = TreeCache if tree else dict
|
cache_type = TreeCache if tree else dict
|
||||||
self._pending_deferred_cache = cache_type()
|
self._pending_deferred_cache = cache_type()
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from typing import Callable, Optional, Type, Union
|
||||||
|
|
||||||
from synapse.config import cache as cache_config
|
from synapse.config import cache as cache_config
|
||||||
from synapse.util.caches.treecache import TreeCache
|
from synapse.util.caches.treecache import TreeCache
|
||||||
|
@ -52,18 +53,18 @@ class LruCache(object):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
max_size,
|
max_size: int,
|
||||||
keylen=1,
|
keylen: int = 1,
|
||||||
cache_type=dict,
|
cache_type: Type[Union[dict, TreeCache]] = dict,
|
||||||
size_callback=None,
|
size_callback: Optional[Callable] = None,
|
||||||
evicted_callback=None,
|
evicted_callback: Optional[Callable] = None,
|
||||||
apply_cache_factor_from_config=True,
|
apply_cache_factor_from_config: bool = True,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
max_size (int):
|
max_size: The maximum amount of entries the cache can hold
|
||||||
|
|
||||||
keylen (int):
|
keylen: The length of the tuple used as the cache key
|
||||||
|
|
||||||
cache_type (type):
|
cache_type (type):
|
||||||
type of underlying cache to be used. Typically one of dict
|
type of underlying cache to be used. Typically one of dict
|
||||||
|
@ -76,7 +77,7 @@ class LruCache(object):
|
||||||
entry
|
entry
|
||||||
|
|
||||||
apply_cache_factor_from_config (bool): If true, `max_size` will be
|
apply_cache_factor_from_config (bool): If true, `max_size` will be
|
||||||
multiplied by a cache factor derived from the homeserver config.
|
multiplied by a cache factor derived from the homeserver config
|
||||||
"""
|
"""
|
||||||
cache = cache_type()
|
cache = cache_type()
|
||||||
self.cache = cache # Used for introspection.
|
self.cache = cache # Used for introspection.
|
||||||
|
|
Loading…
Reference in New Issue