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
This commit is contained in:
Bartlomiej Wolynczyk 2020-01-20 13:32:49 +01:00
parent 34bd899434
commit 347621778d
2 changed files with 17 additions and 5 deletions

View file

@ -288,7 +288,7 @@ def attach_context_attributes(context, request, user_id=None):
"""
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')

View file

@ -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: