1个不稳定版本
0.1.0 | 2023年9月18日 |
---|
#219 in 构建工具
20KB
426 行
FS Dir Cache
一个适用于CI和构建脚本的CLI工具,使基于文件系统的缓存变得简单和正确(锁定、驱逐等)
在构建系统/CI上工作时,通常需要利用一些最佳努力缓存,包括锁定和驱逐。
这并不是什么火箭科学,但非平凡到不愿意实施和维护。
fs-dir-cache
旨在从其他脚本和程序内部使用,从而处理细节。
用例
CI缓存
假设您有一个可以持久化文件之间的运行的CI运行器,并且您希望利用它来重用和加速某些事情
set -euo pipefail
FS_DIR_CACHE_ROOT="$HOME/.cache/fs-dir-cache" # directory to hold all cache (sub)directories
FS_DIR_CACHE_LOCK_ID="pid-$$-rnd-$RANDOM" # acquire lock based on the current pid and something random (just in case pid gets reused)
FS_DIR_CACHE_KEY_NAME="build-project-x" # the base name of our key
FS_DIR_CACHE_LOCK_TIMEOUT_SECS="600" # unlock after timeout in case our job fails misereably
fs-dir-cache gc unused --seconds "$((7 * 24 * 60 * 60))" # delete caches not used in more than a week
# create/reuse cache (sub-directory) and lock it (wait if already locked)
cache_dir=$(fs-dir-cache lock --key-file Cargo.toml)
# unlock it when the script finish
trap "fs-dir-cache unlock --dir ${cache_dir}" EXIT
# 'cache_dir' will now equal to something like '/home/user/.cache/fs-dir-cache/build-project-x-8jg9hsadjfkaj9jkfljdfsd'
# and script has up to 600s to use it exclusively
# build project
cargo build --target-dir="${cache_dir}/target"
使用单个工具,可以轻松获得正确和实用的缓存,包括
- 锁定(包括后备超时)
- 驱逐
- 超时
依赖关系
~12MB
~208K SLoC