1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// Adapted from https://github.com/thought-machine/please
// Copyright Thought Machine, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package cache implements our cache abstraction.
package cache
import (
"encoding/json"
"fmt"
"github.com/vercel/turbo/cli/internal/analytics"
"github.com/vercel/turbo/cli/internal/cacheitem"
"github.com/vercel/turbo/cli/internal/turbopath"
)
// fsCache is a local filesystem cache
type fsCache struct {
cacheDirectory turbopath.AbsoluteSystemPath
recorder analytics.Recorder
}
// newFsCache creates a new filesystem cache
func newFsCache(opts Opts, recorder analytics.Recorder, repoRoot turbopath.AbsoluteSystemPath) (*fsCache, error) {
cacheDir := opts.resolveCacheDir(repoRoot)
if err := cacheDir.MkdirAll(0775); err != nil {
return nil, err
}
return &fsCache{
cacheDirectory: cacheDir,
recorder: recorder,
}, nil
}
// Fetch returns true if items are cached. It moves them into position as a side effect.
func (f *fsCache) Fetch(anchor turbopath.AbsoluteSystemPath, hash string, _ []string) (ItemStatus, []turbopath.AnchoredSystemPath, int, error) {
uncompressedCachePath := f.cacheDirectory.UntypedJoin(hash + ".tar")
compressedCachePath := f.cacheDirectory.UntypedJoin(hash + ".tar.zst")
var actualCachePath turbopath.AbsoluteSystemPath
if uncompressedCachePath.FileExists() {
actualCachePath = uncompressedCachePath
} else if compressedCachePath.FileExists() {
actualCachePath = compressedCachePath
} else {
// It's not in the cache, bail now
f.logFetch(false, hash, 0)
return ItemStatus{Local: false}, nil, 0, nil
}
cacheItem, openErr := cacheitem.Open(actualCachePath)
if openErr != nil {
return ItemStatus{Local: false}, nil, 0, openErr
}
restoredFiles, restoreErr := cacheItem.Restore(anchor)
if restoreErr != nil {
_ = cacheItem.Close()
return ItemStatus{Local: false}, nil, 0, restoreErr
}
meta, err := ReadCacheMetaFile(f.cacheDirectory.UntypedJoin(hash + "-meta.json"))
if err != nil {
_ = cacheItem.Close()
return ItemStatus{Local: false}, nil, 0, fmt.Errorf("error reading cache metadata: %w", err)
}
f.logFetch(true, hash, meta.Duration)
// Wait to see what happens with close.
closeErr := cacheItem.Close()
if closeErr != nil {
return ItemStatus{Local: false}, restoredFiles, 0, closeErr
}
return ItemStatus{Local: true}, restoredFiles, meta.Duration, nil
}
func (f *fsCache) Exists(hash string) ItemStatus {
uncompressedCachePath := f.cacheDirectory.UntypedJoin(hash + ".tar")
compressedCachePath := f.cacheDirectory.UntypedJoin(hash + ".tar.zst")
if compressedCachePath.FileExists() || uncompressedCachePath.FileExists() {
return ItemStatus{Local: true}
}
return ItemStatus{Local: false}
}
func (f *fsCache) logFetch(hit bool, hash string, duration int) {
var event string
if hit {
event = CacheEventHit
} else {
event = CacheEventMiss
}
payload := &CacheEvent{
Source: CacheSourceFS,
Event: event,
Hash: hash,
Duration: duration,
}
f.recorder.LogEvent(payload)
}
func (f *fsCache) Put(anchor turbopath.AbsoluteSystemPath, hash string, duration int, files []turbopath.AnchoredSystemPath) error {
cachePath := f.cacheDirectory.UntypedJoin(hash + ".tar.zst")
cacheItem, err := cacheitem.Create(cachePath)
if err != nil {
return err
}
for _, file := range files {
err := cacheItem.AddFile(anchor, file)
if err != nil {
_ = cacheItem.Close()
return err
}
}
writeErr := WriteCacheMetaFile(f.cacheDirectory.UntypedJoin(hash+"-meta.json"), &CacheMetadata{
Duration: duration,
Hash: hash,
})
if writeErr != nil {
_ = cacheItem.Close()
return writeErr
}
return cacheItem.Close()
}
func (f *fsCache) Clean(_ turbopath.AbsoluteSystemPath) {
fmt.Println("Not implemented yet")
}
func (f *fsCache) CleanAll() {
fmt.Println("Not implemented yet")
}
func (f *fsCache) Shutdown() {}
// CacheMetadata stores duration and hash information for a cache entry so that aggregate Time Saved calculations
// can be made from artifacts from various caches
type CacheMetadata struct {
Hash string `json:"hash"`
Duration int `json:"duration"`
}
// WriteCacheMetaFile writes cache metadata file at a path
func WriteCacheMetaFile(path turbopath.AbsoluteSystemPath, config *CacheMetadata) error {
jsonBytes, marshalErr := json.Marshal(config)
if marshalErr != nil {
return marshalErr
}
writeFilErr := path.WriteFile(jsonBytes, 0644)
if writeFilErr != nil {
return writeFilErr
}
return nil
}
// ReadCacheMetaFile reads cache metadata file at a path
func ReadCacheMetaFile(path turbopath.AbsoluteSystemPath) (*CacheMetadata, error) {
jsonBytes, readFileErr := path.ReadFile()
if readFileErr != nil {
return nil, readFileErr
}
var config CacheMetadata
marshalErr := json.Unmarshal(jsonBytes, &config)
if marshalErr != nil {
return nil, marshalErr
}
return &config, nil
}
|