# HG changeset patch # User RhodeCode Admin # Date 2023-01-13 06:43:17 # Node ID 58a1f157732ca05c956fbd567bc23a1e2c8e891a # Parent ca02973590d1d9b7ebb938b2401ff9675b332ad6 caches: use a better logic to clear cache keys diff --git a/rhodecode/lib/rc_cache/cache_key_meta.py b/rhodecode/lib/rc_cache/cache_key_meta.py --- a/rhodecode/lib/rc_cache/cache_key_meta.py +++ b/rhodecode/lib/rc_cache/cache_key_meta.py @@ -21,14 +21,14 @@ import os import atexit import logging +import signal log = logging.getLogger(__name__) -cache_keys_by_pid = [] +cache_keys_by_pid = set() -@atexit.register -def free_cache_keys(): +def free_cache_keys(*args): ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER') if ssh_cmd: return @@ -38,8 +38,14 @@ def free_cache_keys(): if cache_keys_by_pid: try: - for cache_key in cache_keys_by_pid: - CacheKey.query().filter(CacheKey.cache_key == cache_key).delete() + for cache_proc in cache_keys_by_pid: + CacheKey.query().filter(CacheKey.cache_key.startswith(cache_proc)).delete() Session().commit() + cache_keys_by_pid.clear() except Exception: log.warn('Failed to clear keys, exiting gracefully') + + +atexit.register(free_cache_keys) +signal.signal(signal.SIGTERM, free_cache_keys) +signal.signal(signal.SIGINT, free_cache_keys) diff --git a/rhodecode/lib/rc_cache/utils.py b/rhodecode/lib/rc_cache/utils.py --- a/rhodecode/lib/rc_cache/utils.py +++ b/rhodecode/lib/rc_cache/utils.py @@ -354,6 +354,7 @@ class InvalidationContext(object): self.cache_key = compute_key_from_params(uid) self.cache_key = 'proc:{}|thread:{}|params:{}'.format( self.proc_id, self.thread_id, self.cache_key) + self.proc_key = 'proc:{}'.format(self.proc_id) self.compute_time = 0 def get_or_create_cache_obj(self, cache_type, invalidation_namespace=''): @@ -372,7 +373,7 @@ class InvalidationContext(object): cache_state_uid = first_cache_obj.cache_state_uid cache_obj = CacheKey(self.cache_key, cache_args=new_cache_args, cache_state_uid=cache_state_uid) - cache_key_meta.cache_keys_by_pid.append(self.cache_key) + cache_key_meta.cache_keys_by_pid.add(self.proc_key) return cache_obj