github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/cache/cache.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package cache implements a build artifact cache. 6 package cache 7 8 import ( 9 "github.com/shogo82148/std/io" 10 "github.com/shogo82148/std/time" 11 ) 12 13 // An ActionID is a cache action key, the hash of a complete description of a 14 // repeatable computation (command line, environment variables, 15 // input file contents, executable contents). 16 type ActionID [HashSize]byte 17 18 // An OutputID is a cache output key, the hash of an output of a computation. 19 type OutputID [HashSize]byte 20 21 // Cache is the interface as used by the cmd/go. 22 type Cache interface { 23 Get(ActionID) (Entry, error) 24 25 Put(ActionID, io.ReadSeeker) (_ OutputID, size int64, _ error) 26 27 Close() error 28 29 OutputFile(OutputID) string 30 31 FuzzDir() string 32 } 33 34 // A Cache is a package cache, backed by a file system directory tree. 35 type DiskCache struct { 36 dir string 37 now func() time.Time 38 } 39 40 // Open opens and returns the cache in the given directory. 41 // 42 // It is safe for multiple processes on a single machine to use the 43 // same cache directory in a local file system simultaneously. 44 // They will coordinate using operating system file locks and may 45 // duplicate effort but will not corrupt the cache. 46 // 47 // However, it is NOT safe for multiple processes on different machines 48 // to share a cache directory (for example, if the directory were stored 49 // in a network file system). File locking is notoriously unreliable in 50 // network file systems and may not suffice to protect the cache. 51 func Open(dir string) (*DiskCache, error) 52 53 // DebugTest is set when GODEBUG=gocachetest=1 is in the environment. 54 var DebugTest = false 55 56 // Get looks up the action ID in the cache, 57 // returning the corresponding output ID and file size, if any. 58 // Note that finding an output ID does not guarantee that the 59 // saved file for that output ID is still available. 60 func (c *DiskCache) Get(id ActionID) (Entry, error) 61 62 type Entry struct { 63 OutputID OutputID 64 Size int64 65 Time time.Time 66 } 67 68 // GetFile looks up the action ID in the cache and returns 69 // the name of the corresponding data file. 70 func GetFile(c Cache, id ActionID) (file string, entry Entry, err error) 71 72 // GetBytes looks up the action ID in the cache and returns 73 // the corresponding output bytes. 74 // GetBytes should only be used for data that can be expected to fit in memory. 75 func GetBytes(c Cache, id ActionID) ([]byte, Entry, error) 76 77 // GetMmap looks up the action ID in the cache and returns 78 // the corresponding output bytes. 79 // GetMmap should only be used for data that can be expected to fit in memory. 80 func GetMmap(c Cache, id ActionID) ([]byte, Entry, error) 81 82 // OutputFile returns the name of the cache file storing output with the given OutputID. 83 func (c *DiskCache) OutputFile(out OutputID) string 84 85 func (c *DiskCache) Close() error 86 87 // Trim removes old cache entries that are likely not to be reused. 88 func (c *DiskCache) Trim() error 89 90 // Put stores the given output in the cache as the output for the action ID. 91 // It may read file twice. The content of file must not change between the two passes. 92 func (c *DiskCache) Put(id ActionID, file io.ReadSeeker) (OutputID, int64, error) 93 94 // PutNoVerify is like Put but disables the verify check 95 // when GODEBUG=goverifycache=1 is set. 96 // It is meant for data that is OK to cache but that we expect to vary slightly from run to run, 97 // like test output containing times and the like. 98 func PutNoVerify(c Cache, id ActionID, file io.ReadSeeker) (OutputID, int64, error) 99 100 // PutBytes stores the given bytes in the cache as the output for the action ID. 101 func PutBytes(c Cache, id ActionID, data []byte) error 102 103 // FuzzDir returns a subdirectory within the cache for storing fuzzing data. 104 // The subdirectory may not exist. 105 // 106 // This directory is managed by the internal/fuzz package. Files in this 107 // directory aren't removed by the 'go clean -cache' command or by Trim. 108 // They may be removed with 'go clean -fuzzcache'. 109 // 110 // TODO(#48526): make Trim remove unused files from this directory. 111 func (c *DiskCache) FuzzDir() string