gocache

package
v0.155.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gocache keeps the Go build cache of the machine's user, which every session's builds share, under a size: the entries used least recently go first, as in Go's own trim, which drops only those unused for five days.

Index

Constants

View Source
const HeldEnv = "BEEKEEPER_GOCACHE_HELD"

HeldEnv names, in a beekeeper run's environment, the cache whose use lock the run holds: a clear started inside the run would wait on its own run.

Variables

View Source
var ErrBusy = errors.New("a go build runs")

ErrBusy stops a trim: a Go build began while it ran.

Functions

func Dir

func Dir() string

Dir is the build cache the go command uses: $GOCACHE, else go-build in the user's cache directory; "" when it is off or unknown.

func Hold added in v0.155.0

func Hold(ctx context.Context, dir string, waiting func()) (release func(), err error)

Hold takes the cache's use lock exclusive for a clear: it waits until no command holds it (Use) and no trim runs, until ctx ends, and calls waiting once when it has to wait. Runs that start meanwhile wait behind it. release gives both locks back.

func ModDir added in v0.155.0

func ModDir() string

ModDir is the module cache the go command uses: $GOMODCACHE, else pkg/mod in the first $GOPATH entry, else in go under the home directory; "" when unknown.

func Trim

func Trim(u Usage, target int64, busy func() bool) (freed int64, removed int, err error)

Trim removes the least recently used entries until the cache holds at most target bytes, and returns the bytes and entries it removed. It asks busy before it starts and every few hundred removals, and stops with ErrBusy once a build runs. An entry gone already is skipped; the go command takes a missing entry for a miss and builds it again.

func TryTrim added in v0.149.0

func TryTrim(dir string) (release func(), ok bool, err error)

TryTrim takes the cache's use lock exclusive for a trim, without waiting: ok false while a command holds it (Use).

func Use added in v0.149.0

func Use(ctx context.Context, dir string) (release func(), err error)

Use takes the cache's use lock shared for a command that may compile: it waits for a trim or a clear under way or waiting, until ctx ends. Many commands hold it at once; release gives it back.

Types

type Entry

type Entry struct {
	Path string
	Size int64
	// Used is its modification time, which the go command moves on when it
	// uses an entry last touched over an hour before.
	Used time.Time
}

Entry is one file of the cache: an action or an output.

type Usage

type Usage struct {
	Dir     string
	Entries []Entry
	Bytes   int64
}

Usage is what a scan found: the entries, least recently used first, and their total size.

func Scan

func Scan(dir string) (Usage, error)

Scan reads the cache's entries: the files of its 256 two-hex-digit subdirectories. Everything else (its README, trim.txt, fuzz corpora) is left out, and a missing cache is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL