# HG changeset patch # User RhodeCode Admin # Date 2024-10-23 20:10:33 # Node ID cbb3f19beed47a59c41b8c3269449b659f7b0168 # Parent 22a47fab464128efcc4c238e93de9c1c9cf4d504 feat(redis): added redis prefix support for cache backend diff --git a/configs/development.ini b/configs/development.ini --- a/configs/development.ini +++ b/configs/development.ini @@ -121,6 +121,10 @@ vcs.svn.redis_conn = redis://redis:6379/ ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.repo_object.arguments.lock_auto_renewal = true +; prefix for redis keys used for this cache backend, the final key is constructed using {custom-prefix}{key} +#rc_cache.repo_object.arguments.key_prefix = custom-prefix- + + ; Statsd client config, this is used to send metrics to statsd ; We recommend setting statsd_exported and scrape them using Promethues #statsd.enabled = false diff --git a/configs/production.ini b/configs/production.ini --- a/configs/production.ini +++ b/configs/production.ini @@ -101,6 +101,10 @@ vcs.svn.redis_conn = redis://redis:6379/ ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.repo_object.arguments.lock_auto_renewal = true +; prefix for redis keys used for this cache backend, the final key is constructed using {custom-prefix}{key} +#rc_cache.repo_object.arguments.key_prefix = custom-prefix- + + ; Statsd client config, this is used to send metrics to statsd ; We recommend setting statsd_exported and scrape them using Promethues #statsd.enabled = false diff --git a/vcsserver/lib/rc_cache/backends.py b/vcsserver/lib/rc_cache/backends.py --- a/vcsserver/lib/rc_cache/backends.py +++ b/vcsserver/lib/rc_cache/backends.py @@ -184,6 +184,9 @@ class BaseRedisBackend(redis_backend.Red self._lock_timeout = self.lock_timeout self._lock_auto_renewal = str2bool(arguments.pop("lock_auto_renewal", True)) + self._store_key_prefix = arguments.pop('key_prefix', '') + self.key_prefix = f'{self._store_key_prefix}{self.key_prefix}' + if self._lock_auto_renewal and not self._lock_timeout: # set default timeout for auto_renewal self._lock_timeout = 30 @@ -243,7 +246,7 @@ class BaseRedisBackend(redis_backend.Red def get_mutex(self, key): if self.distributed_lock: - lock_key = f'_lock_{safe_str(key)}' + lock_key = f'{self._store_key_prefix}_lock_{safe_str(key)}' return get_mutex_lock( self.writer_client, lock_key, self._lock_timeout, diff --git a/vcsserver/lib/rc_cache/utils.py b/vcsserver/lib/rc_cache/utils.py --- a/vcsserver/lib/rc_cache/utils.py +++ b/vcsserver/lib/rc_cache/utils.py @@ -69,11 +69,9 @@ class RhodeCodeCacheRegion(CacheRegion): return result key = func_key_generator(*arg, **kw) + timeout = expiration_time() if expiration_time_is_callable else expiration_time + log.debug('Calling cached (timeout=%s) method:`%s`', timeout, user_func.__name__) - timeout = expiration_time() if expiration_time_is_callable \ - else expiration_time - - log.debug('Calling cached method:`%s`', user_func.__name__) return self.get_or_create(key, user_func, timeout, should_cache_fn, (arg, kw)) def cache_decorator(user_func):