Skip to content

Instantly share code, notes, and snippets.

@sha1n
Last active September 28, 2021 13:44
Show Gist options
  • Save sha1n/fbb072ed3f03c921fab2c4f8c0678854 to your computer and use it in GitHub Desktop.
Save sha1n/fbb072ed3f03c921fab2c4f8c0678854 to your computer and use it in GitHub Desktop.
Poor man's Bazel disk-cache age based eviction

Poor man's Bazel disk-cache age based eviction

Usage:

  1. Edit BAZEL_BIN_PATH in the script to match your setup
  2. Add build --disk_cache=~/.cache/bazel/disk_cache to your .bazelrc. Edit the path to your preferences and the variable value accordingly.
  3. Edit the value of BAZEL_DISK_CACHE_MAX_AGE to match your preferences.
  • Higher values means more cache hits over time
  • Lower values means less disk-space and less cleanup time and CPU load
  1. Finally source the scriupt from your shell profile
#!/usr/bin/env zsh
export BAZEL_BIN_PATH="$HOME/.local/bin/bazel" # edit according to your system
export BAZEL_DISK_CACHE_DIR="$HOME/.cache/bazel/disk_cache" # ~/.bazelrc <- build --disk_cache=~/.cache/bazel/disk_cache
export BAZEL_DISK_CACHE_MAX_AGE="14d" # edit to your prefs
function bazel_with_disk_cache_guard() {
("$BAZEL_BIN_PATH" "$@")
local rc="$?"
(find "$BAZEL_DISK_CACHE_DIR" -type f -mtime +$BAZEL_DISK_CACHE_MAX_AGE -delete &)
if [[ "$?" != "0" ]]; then
echo "WARNING: [${funcstack[1]}] disk cache cleanup failed"
fi
return "$rc"
}
alias bazel='bazel_with_disk_cache_guard'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment