# HG changeset patch # User Bartlomiej Wolynczyk # Date 2020-01-20 12:32:49 # Node ID 007322e6451dd85be68ccbb26e559f51fcb55d51 # Parent 9041744d4ca3c964025ef5c333d37da69f3f354b settings: re-use attached request global config for performance - since we *always* generate a global config and register it into request make use of it, and not rely on the get_all_settings heavy logic that makes it slow to fetch because of cache checks. Within single request we're fine to re-use it when ever request attribute is available diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -288,7 +288,7 @@ def attach_context_attributes(context, r """ config = request.registry.settings - rc_config = SettingsModel().get_all_settings(cache=True) + rc_config = SettingsModel().get_all_settings(cache=True, from_request=False) context.rc_config = rc_config context.rhodecode_version = rhodecode.__version__ context.rhodecode_edition = config.get('rhodecode.edition') diff --git a/rhodecode/model/settings.py b/rhodecode/model/settings.py --- a/rhodecode/model/settings.py +++ b/rhodecode/model/settings.py @@ -24,6 +24,7 @@ import logging from collections import namedtuple from functools import wraps import bleach +from pyramid.threadlocal import get_current_request from rhodecode.lib import rc_cache from rhodecode.lib.utils2 import ( @@ -210,7 +211,21 @@ class SettingsModel(BaseModel): invalidation_namespace = CacheKey.SETTINGS_INVALIDATION_NAMESPACE CacheKey.set_invalidate(invalidation_namespace) - def get_all_settings(self, cache=False): + def get_all_settings(self, cache=False, from_request=True): + # defines if we use GLOBAL, or PER_REPO + repo = self._get_repo(self.repo) if self.repo else None + key = "settings_repo.{}".format(repo.repo_id) if repo else "settings_app" + + # initially try the requests context, this is the fastest + # we only fetch global config + if from_request: + request = get_current_request() + + if request and not repo and hasattr(request, 'call_context') and hasattr(request.call_context, 'rc_config'): + rc_config = request.call_context.rc_config + if rc_config: + return rc_config + region = rc_cache.get_or_create_region('sql_cache_short') invalidation_namespace = CacheKey.SETTINGS_INVALIDATION_NAMESPACE @@ -226,9 +241,6 @@ class SettingsModel(BaseModel): } return settings - repo = self._get_repo(self.repo) if self.repo else None - key = "settings_repo.{}".format(repo.repo_id) if repo else "settings_app" - inv_context_manager = rc_cache.InvalidationContext( uid='cache_settings', invalidation_namespace=invalidation_namespace) with inv_context_manager as invalidation_context: