@@ -25,26 +25,31 @@ import (
2525
2626const  lastUsedFileName  =  ".last-used" 
2727
28+ type  buildCache  struct  {
29+ 	baseDir  * paths.Path 
30+ }
31+ 
2832// GetOrCreate retrieves or creates the cache directory at the given path 
2933// 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  {
3338			return  nil , err 
3439		}
3540	}
3641
37- 	if  err  :=  dir .Join (lastUsedFileName ).WriteFile ([]byte {}); err  !=  nil  {
42+ 	if  err  :=  keyDir .Join (lastUsedFileName ).WriteFile ([]byte {}); err  !=  nil  {
3843		return  nil , err 
3944	}
40- 	return  dir , nil 
45+ 	return  keyDir , nil 
4146}
4247
4348// Purge removes all cache directories within baseDir that have expired 
4449// To know how long ago a directory has been last used 
4550// 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 ()
4853	if  err  !=  nil  {
4954		return 
5055	}
@@ -55,6 +60,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) {
5560	}
5661}
5762
63+ // New instantiates a build cache 
64+ func  New (baseDir  * paths.Path ) * buildCache  {
65+ 	return  & buildCache {baseDir }
66+ }
67+ 
5868func  removeIfExpired (dir  * paths.Path , ttl  time.Duration ) {
5969	fileInfo , err  :=  dir .Join ().Stat ()
6070	if  err  !=  nil  {
0 commit comments