# HG changeset patch # User RhodeCode Admin # Date 2024-06-06 12:43:26 # Node ID 386f3a638cc3bbaff7bf39a2d4daf6998ddf58bb # Parent 0968de67b7400601ab1dd41ce065b4289b920ea7 feat(archive-cache): implement more usage stats for later easier evictions diff --git a/rhodecode/lib/rc_cache/archive_cache/fanout_cache.py b/rhodecode/lib/rc_cache/archive_cache/fanout_cache.py --- a/rhodecode/lib/rc_cache/archive_cache/fanout_cache.py +++ b/rhodecode/lib/rc_cache/archive_cache/fanout_cache.py @@ -78,10 +78,13 @@ class FileSystemCache: # STORE METADATA _metadata = { "version": "v1", - "timestamp": time.time(), "filename": filename, "full_path": full_path, "key_file": key_file, + "store_time": time.time(), + "access_count": 1, + "access_time": 0, + "size": 0 } if metadata: _metadata.update(metadata) @@ -90,6 +93,7 @@ class FileSystemCache: iterator = iter(reader, b'') size = self._write_file(full_path, iterator, 'xb') + metadata['size'] = size # after archive is finished, we create a key to save the presence of the binary file with open(key_file, 'wb') as f: @@ -107,7 +111,15 @@ class FileSystemCache: filename = metadata['filename'] - return open(os.path.join(self._directory, filename), 'rb'), metadata + try: + return open(os.path.join(self._directory, filename), 'rb'), metadata + finally: + # update usage stats, count and accessed + metadata["access_count"] = metadata.get("access_count", 0) + 1 + metadata["access_time"] = time.time() + + with open(key_file, 'wb') as f: + f.write(json.dumps(metadata)) def random_filename(self): """Return filename and full-path tuple for file storage. @@ -169,6 +181,9 @@ class FanoutCache: self._count = settings.pop('cache_shards') self._locking_url = settings.pop('locking_url') + self._eviction_policy = settings['cache_eviction_policy'] + self._cache_size_limit = settings['cache_size_limit'] + self._shards = tuple( FileSystemCache( index=num, @@ -261,4 +276,3 @@ def get_archival_cache_store(config): ) cache_meta = d_cache return cache_meta - diff --git a/rhodecode/lib/rc_cache/archive_cache/utils.py b/rhodecode/lib/rc_cache/archive_cache/utils.py --- a/rhodecode/lib/rc_cache/archive_cache/utils.py +++ b/rhodecode/lib/rc_cache/archive_cache/utils.py @@ -15,6 +15,7 @@ # This program is dual-licensed. If you wish to learn more about the # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ + import os