@@ -25,26 +25,29 @@ import (
25
25
26
26
const lastUsedFileName = ".last-used"
27
27
28
- // GetOrCreate retrieves or creates the cache directory at the given path
29
- // If the cache already exists the lifetime of the cache is extended.
30
- func GetOrCreate (dir * paths.Path ) (* paths.Path , error ) {
31
- if ! dir .Exist () {
32
- if err := dir .MkdirAll (); err != nil {
28
+ type buildCache struct {
29
+ baseDir * paths.Path
30
+ }
31
+
32
+ func (bc * buildCache ) GetOrCreate (key string ) (* paths.Path , error ) {
33
+ keyDir := bc .baseDir .Join (key )
34
+ if ! keyDir .Exist () {
35
+ if err := keyDir .MkdirAll (); err != nil {
33
36
return nil , err
34
37
}
35
38
}
36
39
37
- if err := dir .Join (lastUsedFileName ).WriteFile ([]byte {}); err != nil {
40
+ if err := keyDir .Join (lastUsedFileName ).WriteFile ([]byte {}); err != nil {
38
41
return nil , err
39
42
}
40
- return dir , nil
43
+ return keyDir , nil
41
44
}
42
45
43
46
// Purge removes all cache directories within baseDir that have expired
44
47
// To know how long ago a directory has been last used
45
48
// it checks into the .last-used file.
46
- func Purge ( baseDir * paths. Path , ttl time.Duration ) {
47
- files , err := baseDir .ReadDir ()
49
+ func ( bc * buildCache ) Purge ( ttl time.Duration ) {
50
+ files , err := bc . baseDir .ReadDir ()
48
51
if err != nil {
49
52
return
50
53
}
@@ -55,6 +58,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) {
55
58
}
56
59
}
57
60
61
+ // New instantiates a build cache
62
+ func New (baseDir * paths.Path ) * buildCache {
63
+ return & buildCache {baseDir }
64
+ }
65
+
58
66
func removeIfExpired (dir * paths.Path , ttl time.Duration ) {
59
67
fileInfo , err := dir .Join ().Stat ()
60
68
if err != nil {
0 commit comments