From 7d196811c46dcbf329b3f7fd67933d1a2ddb6322 Mon Sep 17 00:00:00 2001 From: RhodeCode Admin Date: Wed, 23 Oct 2024 22:11:27 +0200 Subject: [PATCH] feat(redis): added ability to specify a redis key prefix for all cache regions of dogpile that use redis backend --- configs/development.ini | 12 ++++++++++++ configs/production.ini | 12 ++++++++++++ rhodecode/lib/rc_cache/backends.py | 5 ++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/configs/development.ini b/configs/development.ini index 5d4100b7..096ebc45 100644 --- a/configs/development.ini +++ b/configs/development.ini @@ -485,6 +485,10 @@ rc_cache.cache_general.expiration_time = 43200 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_general.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.cache_general.arguments.key_prefix = custom-prefix- + + ; ************************************************* ; `cache_perms` cache for permission tree, auth TTL ; for simplicity use rc.file_namespace backend, @@ -512,6 +516,10 @@ rc_cache.cache_perms.expiration_time = 3600 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_perms.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.cache_perms.arguments.key_prefix = custom-prefix- + + ; *************************************************** ; `cache_repo` cache for file tree, Readme, RSS FEEDS ; for simplicity use rc.file_namespace backend, @@ -539,6 +547,10 @@ rc_cache.cache_repo.expiration_time = 2592000 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_repo.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.cache_repo.arguments.key_prefix = custom-prefix- + + ; ############## ; BEAKER SESSION ; ############## diff --git a/configs/production.ini b/configs/production.ini index 8a2713cf..073943ca 100644 --- a/configs/production.ini +++ b/configs/production.ini @@ -453,6 +453,10 @@ rc_cache.cache_general.expiration_time = 43200 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_general.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.cache_general.arguments.key_prefix = custom-prefix- + + ; ************************************************* ; `cache_perms` cache for permission tree, auth TTL ; for simplicity use rc.file_namespace backend, @@ -480,6 +484,10 @@ rc_cache.cache_perms.expiration_time = 3600 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_perms.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.cache_perms.arguments.key_prefix = custom-prefix- + + ; *************************************************** ; `cache_repo` cache for file tree, Readme, RSS FEEDS ; for simplicity use rc.file_namespace backend, @@ -507,6 +515,10 @@ rc_cache.cache_repo.expiration_time = 2592000 ; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen #rc_cache.cache_repo.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.cache_repo.arguments.key_prefix = custom-prefix- + + ; ############## ; BEAKER SESSION ; ############## diff --git a/rhodecode/lib/rc_cache/backends.py b/rhodecode/lib/rc_cache/backends.py index b4f64aa5..04d4f724 100644 --- a/rhodecode/lib/rc_cache/backends.py +++ b/rhodecode/lib/rc_cache/backends.py @@ -216,6 +216,9 @@ class BaseRedisBackend(redis_backend.RedisBackend): 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 @@ -275,7 +278,7 @@ class BaseRedisBackend(redis_backend.RedisBackend): 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,