diff --git a/rhodecode/config/middleware.py b/rhodecode/config/middleware.py --- a/rhodecode/config/middleware.py +++ b/rhodecode/config/middleware.py @@ -207,6 +207,7 @@ def includeme(config): # Includes which are required. The application would fail without them. config.include('pyramid_mako') config.include('pyramid_beaker') + config.include('rhodecode.lib.caches') config.include('rhodecode.authentication') config.include('rhodecode.integrations') diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -499,6 +499,7 @@ def bootstrap_config(request): # allow pyramid lookup in testing config.include('pyramid_mako') config.include('pyramid_beaker') + config.include('rhodecode.lib.caches') add_events_routes(config) diff --git a/rhodecode/lib/caches.py b/rhodecode/lib/caches.py --- a/rhodecode/lib/caches.py +++ b/rhodecode/lib/caches.py @@ -47,18 +47,48 @@ DEFAULT_CACHE_MANAGER_CONFIG = { } +def get_default_cache_settings(settings): + cache_settings = {} + for key in settings.keys(): + for prefix in ['beaker.cache.', 'cache.']: + if key.startswith(prefix): + name = key.split(prefix)[1].strip() + cache_settings[name] = settings[key].strip() + return cache_settings + + +# set cache regions for beaker so celery can utilise it +def configure_caches(settings, default_region_settings=None): + cache_settings = {'regions': None} + # main cache settings used as default ... + cache_settings.update(get_default_cache_settings(settings)) + default_region_settings = default_region_settings or \ + {'type': DEFAULT_CACHE_MANAGER_CONFIG['type']} + if cache_settings['regions']: + for region in cache_settings['regions'].split(','): + region = region.strip() + region_settings = default_region_settings.copy() + for key, value in cache_settings.items(): + if key.startswith(region): + region_settings[key.split('.')[1]] = value + log.debug('Configuring cache region `%s` with settings %s', + region, region_settings) + configure_cache_region( + region, region_settings, cache_settings) + + def configure_cache_region( - region_name, region_kw, default_cache_kw, default_expire=60): + region_name, region_settings, default_cache_kw, default_expire=60): default_type = default_cache_kw.get('type', 'memory') default_lock_dir = default_cache_kw.get('lock_dir') default_data_dir = default_cache_kw.get('data_dir') - region_kw['lock_dir'] = region_kw.get('lock_dir', default_lock_dir) - region_kw['data_dir'] = region_kw.get('data_dir', default_data_dir) - region_kw['type'] = region_kw.get('type', default_type) - region_kw['expire'] = int(region_kw.get('expire', default_expire)) + region_settings['lock_dir'] = region_settings.get('lock_dir', default_lock_dir) + region_settings['data_dir'] = region_settings.get('data_dir', default_data_dir) + region_settings['type'] = region_settings.get('type', default_type) + region_settings['expire'] = int(region_settings.get('expire', default_expire)) - beaker.cache.cache_regions[region_name] = region_kw + beaker.cache.cache_regions[region_name] = region_settings def get_cache_manager(region_name, cache_name, custom_ttl=None): @@ -233,3 +263,7 @@ class InvalidationContext(object): Session().rollback() if self.raise_exception: raise + + +def includeme(config): + configure_caches(config.registry.settings) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -619,35 +619,6 @@ def repo2db_mapper(initial_repo_list, re return added, removed -def get_default_cache_settings(settings): - cache_settings = {} - for key in settings.keys(): - for prefix in ['beaker.cache.', 'cache.']: - if key.startswith(prefix): - name = key.split(prefix)[1].strip() - cache_settings[name] = settings[key].strip() - return cache_settings - - -# set cache regions for beaker so celery can utilise it -def add_cache(settings): - from rhodecode.lib import caches - cache_settings = {'regions': None} - # main cache settings used as default ... - cache_settings.update(get_default_cache_settings(settings)) - - if cache_settings['regions']: - for region in cache_settings['regions'].split(','): - region = region.strip() - region_settings = {} - for key, value in cache_settings.items(): - if key.startswith(region): - region_settings[key.split('.')[1]] = value - - caches.configure_cache_region( - region, region_settings, cache_settings) - - def load_rcextensions(root_path): import rhodecode from rhodecode.config import conf diff --git a/rhodecode/tests/rhodecode.ini b/rhodecode/tests/rhodecode.ini --- a/rhodecode/tests/rhodecode.ini +++ b/rhodecode/tests/rhodecode.ini @@ -390,7 +390,7 @@ beaker.cache.sql_cache_short.key_length ## default is memory cache, configure only if required ## using multi-node or multi-worker setup -beaker.cache.auth_plugins.type = memory +#beaker.cache.auth_plugins.type = memory #beaker.cache.auth_plugins.lock_dir = %(here)s/data/cache/auth_plugin_lock #beaker.cache.auth_plugins.url = postgresql://postgres:secret@localhost/rhodecode #beaker.cache.auth_plugins.url = mysql://root:secret@127.0.0.1/rhodecode