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