@@ -25,26 +25,29 @@ import (
2525
2626const lastUsedFileName = ".last-used"
2727
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 {
3336 return nil , err
3437 }
3538 }
3639
37- if err := dir .Join (lastUsedFileName ).WriteFile ([]byte {}); err != nil {
40+ if err := keyDir .Join (lastUsedFileName ).WriteFile ([]byte {}); err != nil {
3841 return nil , err
3942 }
40- return dir , nil
43+ return keyDir , nil
4144}
4245
4346// Purge removes all cache directories within baseDir that have expired
4447// To know how long ago a directory has been last used
4548// 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 ()
4851 if err != nil {
4952 return
5053 }
@@ -55,6 +58,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) {
5558 }
5659}
5760
61+ // New instantiates a build cache
62+ func New (baseDir * paths.Path ) * buildCache {
63+ return & buildCache {baseDir }
64+ }
65+
5866func removeIfExpired (dir * paths.Path , ttl time.Duration ) {
5967 fileInfo , err := dir .Join ().Stat ()
6068 if err != nil {
0 commit comments