type ActionID … type OutputID … type Cache … type DiskCache … // Open opens and returns the cache in the given directory. // // It is safe for multiple processes on a single machine to use the // same cache directory in a local file system simultaneously. // They will coordinate using operating system file locks and may // duplicate effort but will not corrupt the cache. // // However, it is NOT safe for multiple processes on different machines // to share a cache directory (for example, if the directory were stored // in a network file system). File locking is notoriously unreliable in // network file systems and may not suffice to protect the cache. func Open(dir string) (*DiskCache, error) { … } // fileName returns the name of the file corresponding to the given id. func (c *DiskCache) fileName(id [HashSize]byte, key string) string { … } type entryNotFoundError … func (e *entryNotFoundError) Error() string { … } func (e *entryNotFoundError) Unwrap() error { … } const hexSize … const entrySize … var verify … var errVerifyMode … var DebugTest … func init() { … } var gocacheverify … var gocachehash … var gocachetest … func initEnv() { … } // Get looks up the action ID in the cache, // returning the corresponding output ID and file size, if any. // Note that finding an output ID does not guarantee that the // saved file for that output ID is still available. func (c *DiskCache) Get(id ActionID) (Entry, error) { … } type Entry … // get is Get but does not respect verify mode, so that Put can use it. func (c *DiskCache) get(id ActionID) (Entry, error) { … } // GetFile looks up the action ID in the cache and returns // the name of the corresponding data file. func GetFile(c Cache, id ActionID) (file string, entry Entry, err error) { … } // GetBytes looks up the action ID in the cache and returns // the corresponding output bytes. // GetBytes should only be used for data that can be expected to fit in memory. func GetBytes(c Cache, id ActionID) ([]byte, Entry, error) { … } // GetMmap looks up the action ID in the cache and returns // the corresponding output bytes. // GetMmap should only be used for data that can be expected to fit in memory. func GetMmap(c Cache, id ActionID) ([]byte, Entry, error) { … } // OutputFile returns the name of the cache file storing output with the given OutputID. func (c *DiskCache) OutputFile(out OutputID) string { … } const mtimeInterval … const trimInterval … const trimLimit … // used makes a best-effort attempt to update mtime on file, // so that mtime reflects cache access time. // // Because the reflection only needs to be approximate, // and to reduce the amount of disk activity caused by using // cache entries, used only updates the mtime if the current // mtime is more than an hour old. This heuristic eliminates // nearly all of the mtime updates that would otherwise happen, // while still keeping the mtimes useful for cache trimming. func (c *DiskCache) used(file string) { … } func (c *DiskCache) Close() error { … } // Trim removes old cache entries that are likely not to be reused. func (c *DiskCache) Trim() error { … } // trimSubdir trims a single cache subdirectory. func (c *DiskCache) trimSubdir(subdir string, cutoff time.Time) { … } // putIndexEntry adds an entry to the cache recording that executing the action // with the given id produces an output with the given output id (hash) and size. func (c *DiskCache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify bool) error { … } type noVerifyReadSeeker … // Put stores the given output in the cache as the output for the action ID. // It may read file twice. The content of file must not change between the two passes. func (c *DiskCache) Put(id ActionID, file io.ReadSeeker) (OutputID, int64, error) { … } // PutNoVerify is like Put but disables the verify check // when GODEBUG=goverifycache=1 is set. // It is meant for data that is OK to cache but that we expect to vary slightly from run to run, // like test output containing times and the like. func PutNoVerify(c Cache, id ActionID, file io.ReadSeeker) (OutputID, int64, error) { … } func (c *DiskCache) put(id ActionID, file io.ReadSeeker, allowVerify bool) (OutputID, int64, error) { … } // PutBytes stores the given bytes in the cache as the output for the action ID. func PutBytes(c Cache, id ActionID, data []byte) error { … } // copyFile copies file into the cache, expecting it to have the given // output ID and size, if that file is not present already. func (c *DiskCache) copyFile(file io.ReadSeeker, out OutputID, size int64) error { … } // FuzzDir returns a subdirectory within the cache for storing fuzzing data. // The subdirectory may not exist. // // This directory is managed by the internal/fuzz package. Files in this // directory aren't removed by the 'go clean -cache' command or by Trim. // They may be removed with 'go clean -fuzzcache'. // // TODO(#48526): make Trim remove unused files from this directory. func (c *DiskCache) FuzzDir() string { … }