# HG changeset patch # User RhodeCode Admin # Date 2024-08-07 11:41:09 # Node ID ed4052889301fa9eb9dc95fb381d1cf5de7db215 # Parent ce38cffeded8a17c7e7151d36e34762fa14aa727 chore(caches): cleanup rc_cache and implement cleanup_backend for filesystem diff --git a/vcsserver/lib/hash_utils.py b/vcsserver/lib/hash_utils.py new file mode 100644 --- /dev/null +++ b/vcsserver/lib/hash_utils.py @@ -0,0 +1,53 @@ +# RhodeCode VCSServer provides access to different vcs backends via network. +# Copyright (C) 2014-2023 RhodeCode GmbH +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +import hashlib +from vcsserver.lib.str_utils import safe_bytes, safe_str + + +def md5(s): + return hashlib.md5(s).hexdigest() + + +def md5_safe(s, return_type=''): + + val = md5(safe_bytes(s)) + if return_type == 'str': + val = safe_str(val) + return val + + +def sha1(s): + return hashlib.sha1(s).hexdigest() + + +def sha1_safe(s, return_type=''): + val = sha1(safe_bytes(s)) + if return_type == 'str': + val = safe_str(val) + return val + + +def sha256(s): + return hashlib.sha256(s).hexdigest() + + +def sha256_safe(s, return_type=''): + val = sha256(safe_bytes(s)) + if return_type == 'str': + val = safe_str(val) + return val 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 @@ -37,9 +37,9 @@ from dogpile.cache.backends import redis from dogpile.cache.backends.file import FileLock from dogpile.cache.util import memoized_property -from vcsserver.lib.memory_lru_dict import LRUDict, LRUDictDebug -from vcsserver.lib.str_utils import safe_bytes, safe_str -from vcsserver.lib.type_utils import str2bool +from ...lib.memory_lru_dict import LRUDict, LRUDictDebug +from ...lib.str_utils import safe_bytes, safe_str +from ...lib.type_utils import str2bool _default_max_size = 1024 @@ -166,6 +166,13 @@ class FileNamespaceBackend(PickleSeriali def get_store(self): return self.filename + def cleanup_store(self): + for ext in ("db", "dat", "pag", "dir"): + final_filename = self.filename + os.extsep + ext + if os.path.exists(final_filename): + os.remove(final_filename) + log.warning('Removed dbm file %s', final_filename) + class BaseRedisBackend(redis_backend.RedisBackend): key_prefix = '' @@ -257,7 +264,7 @@ class RedisMsgPackBackend(MsgPackSeriali def get_mutex_lock(client, lock_key, lock_timeout, auto_renewal=False): - from vcsserver.lib._vendor import redis_lock + from ...lib._vendor import redis_lock class _RedisLockWrapper: """LockWrapper for redis_lock""" 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 @@ -25,9 +25,9 @@ import decorator from dogpile.cache import CacheRegion -from vcsserver.utils import sha1 -from vcsserver.lib.str_utils import safe_bytes -from vcsserver.lib.type_utils import str2bool # noqa :required by imports from .utils +from ...lib.hash_utils import sha1 +from ...lib.str_utils import safe_bytes +from ...lib.type_utils import str2bool # noqa :required by imports from .utils from . import region_meta