Skip to content

Commit

Permalink
refactor: new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bianconi committed Jan 27, 2023
1 parent 3b11d67 commit 776d66c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
26 changes: 17 additions & 9 deletions buildcache/build_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,29 @@ import (

const lastUsedFileName = ".last-used"

type buildCache struct {
baseDir *paths.Path
}

// GetOrCreate retrieves or creates the cache directory at the given path
// If the cache already exists the lifetime of the cache is extended.
func GetOrCreate(dir *paths.Path) (*paths.Path, error) {
if !dir.Exist() {
if err := dir.MkdirAll(); err != nil {
return nil, err
}
func (bc *buildCache) GetOrCreate(key string) (*paths.Path, error) {
keyDir := bc.baseDir.Join(key)
if err := keyDir.MkdirAll(); err != nil {
return nil, err
}

if err := dir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil {
if err := keyDir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil {
return nil, err
}
return dir, nil
return keyDir, nil
}

// Purge removes all cache directories within baseDir that have expired
// To know how long ago a directory has been last used
// it checks into the .last-used file.
func Purge(baseDir *paths.Path, ttl time.Duration) {
files, err := baseDir.ReadDir()
func (bc *buildCache) Purge(ttl time.Duration) {
files, err := bc.baseDir.ReadDir()
if err != nil {
return
}
Expand All @@ -55,6 +58,11 @@ func Purge(baseDir *paths.Path, ttl time.Duration) {
}
}

// New instantiates a build cache
func New(baseDir *paths.Path) *buildCache {
return &buildCache{baseDir}
}

func removeIfExpired(dir *paths.Path, ttl time.Duration) {
fileInfo, err := dir.Join().Stat()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions buildcache/build_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) {
}

func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
_, err := GetOrCreate(dir)
_, err := New(dir.Parent()).GetOrCreate(dir.Base())
require.NoError(t, err)
expectedFile := dir.Join(lastUsedFileName)
fileInfo, err := expectedFile.Stat()
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestPurge(t *testing.T) {
require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime))
}

Purge(dirToPurge, ttl)
New(dirToPurge).Purge(ttl)

files, err := dirToPurge.Join("fresh").Stat()
require.Nil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
}

buildcache.GetOrCreate(builderCtx.BuildPath)
buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base())
// cache is purged after compilation to not remove entries that might be required
defer maybePurgeBuildCache()

Expand Down Expand Up @@ -312,6 +312,6 @@ func maybePurgeBuildCache() {
}
inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0)
cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs()
buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL)
buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL)
buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL)
buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL)
}
2 changes: 1 addition & 1 deletion legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
archivedCoreName := GetCachedCoreArchiveDirName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
_, buildCacheErr = buildcache.GetOrCreate(targetArchivedCore.Parent())
_, buildCacheErr = buildcache.New(buildCachePath).GetOrCreate(archivedCoreName)

canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase &&
!ctx.Clean &&
Expand Down

0 comments on commit 776d66c

Please sign in to comment.