Skip to content

Commit 78ede0d

Browse files
author
Luca Bianconi
committed
refactor: pr comments
1 parent 6fb6e12 commit 78ede0d

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

Diff for: buildcache/build_cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
// Used registers the .last-used time in the directory
2525
func Used(dir *paths.Path) error {
2626
unusedTTL := time.Hour
27-
_, err := newDirectoryCache(dir.Parent().String(), unusedTTL).
27+
_, err := newDirectoryCache(dir.Parent(), unusedTTL).
2828
GetOrCreate(dir.Base(), time.Now())
2929
return err
3030
}
@@ -33,5 +33,5 @@ func Used(dir *paths.Path) error {
3333
// To know how long ago a directory has been last used
3434
// it checks into the .last-used file.
3535
func Purge(baseDir *paths.Path, ttl time.Duration) {
36-
newDirectoryCache(baseDir.String(), ttl).Purge()
36+
newDirectoryCache(baseDir, ttl).Purge()
3737
}

Diff for: buildcache/build_cache_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time)
4747
expectedFile := dir.Join(lastUsedFileName)
4848
fileInfo, err := expectedFile.Stat()
4949
require.Nil(t, err)
50-
require.GreaterOrEqual(t, fileInfo.ModTime(), prevModTime)
50+
require.Greater(t, fileInfo.ModTime(), prevModTime)
5151
}
5252

5353
func TestPurge(t *testing.T) {

Diff for: buildcache/directory_cache.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ const (
2828
)
2929

3030
type directoryCache struct {
31-
baseDir string
31+
baseDir *paths.Path
3232
ttl time.Duration
3333
}
3434

35-
func (dc *directoryCache) basePath() *paths.Path {
36-
return paths.New(dc.baseDir)
37-
}
38-
3935
func (dc *directoryCache) isExpired(key string) time.Duration {
4036
modTime, _ := dc.modTime(key)
4137
return dc.ttl - time.Since(modTime)
4238
}
4339

4440
func (dc *directoryCache) modTime(key string) (time.Time, error) {
45-
fileInfo, err := dc.basePath().Join(key, lastUsedFileName).Stat()
41+
fileInfo, err := dc.baseDir.Join(key, lastUsedFileName).Stat()
4642
if err != nil {
4743
// folders with a missing last used file are not purged
4844
return time.Now().Add(time.Minute), err
@@ -58,7 +54,7 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
5854
existing = false
5955
}
6056

61-
subDir := dc.basePath().Join(key)
57+
subDir := dc.baseDir.Join(key)
6258
err = subDir.MkdirAll()
6359
if err != nil || existing {
6460
return modTime, err
@@ -71,7 +67,7 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
7167
}
7268

7369
func (dc *directoryCache) Purge() error {
74-
files, err := dc.basePath().ReadDir()
70+
files, err := dc.baseDir.ReadDir()
7571
if err != nil {
7672
return err
7773
}
@@ -88,16 +84,15 @@ func (dc *directoryCache) removeIfExpired(dir *paths.Path) {
8884
if lifeExpectancy > 0 {
8985
return
9086
}
91-
subDir := dc.basePath().Join(dir.Base())
92-
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, subDir, lifeExpectancy)
93-
err := subDir.RemoveAll()
87+
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, dir, lifeExpectancy)
88+
err := dir.RemoveAll()
9489

9590
if err != nil {
96-
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, subDir, errors.WithStack(err))
91+
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, dir, errors.WithStack(err))
9792
}
9893
}
9994

100-
func newDirectoryCache(baseDir string, ttl time.Duration) *directoryCache {
95+
func newDirectoryCache(baseDir *paths.Path, ttl time.Duration) *directoryCache {
10196
return &directoryCache{
10297
baseDir: baseDir,
10398
ttl: ttl,

Diff for: commands/compile/compile.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,19 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
278278
// maybePurgeBuildCache runs the build files cache purge if the policy conditions are met.
279279
func maybePurgeBuildCache() {
280280

281-
compilationSinceLastPurge := inventory.Store.GetInt("build_cache.compilation_count_since_last_purge")
281+
compilationsBeforePurge := configuration.Settings.GetUint("build_cache.compilations_before_purge")
282+
// 0 means never purge
283+
if compilationsBeforePurge == 0 {
284+
return
285+
}
286+
compilationSinceLastPurge := inventory.Store.GetUint("build_cache.compilation_count_since_last_purge")
282287
compilationSinceLastPurge++
283288
inventory.Store.Set("build_cache.compilation_count_since_last_purge", compilationSinceLastPurge)
284289
defer inventory.WriteStore()
285-
286-
// 0 means never purge
287-
purgeAfterCompilationCount := configuration.Settings.GetInt("build_cache.compilations_before_purge")
288-
289-
if purgeAfterCompilationCount == 0 || compilationSinceLastPurge < purgeAfterCompilationCount {
290+
if compilationsBeforePurge == 0 || compilationSinceLastPurge < compilationsBeforePurge {
290291
return
291292
}
292293
inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0)
293-
294294
cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs()
295295
buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL)
296296
buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL)

Diff for: internal/integrationtest/compile_1/compile_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func compileWithCachePurgeNeeded(t *testing.T, env *integrationtest.Environment,
124124

125125
// purge case: last used file too old
126126
oldDir1 := baseDir.Join("test_old_sketch_1")
127-
require.NoError(t, os.MkdirAll(oldDir1.String(), 0770))
127+
require.NoError(t, oldDir1.MkdirAll())
128128
require.NoError(t, oldDir1.Join(".last-used").WriteFile([]byte{}))
129-
require.NoError(t, os.Chtimes(oldDir1.Join(".last-used").String(), time.Now(), time.Unix(0, 0)))
129+
require.NoError(t, oldDir1.Join(".last-used").Chtimes(time.Now(), time.Unix(0, 0)))
130130
// no purge case: last used file not existing
131131
missingFileDir := baseDir.Join("test_sketch_2")
132-
require.NoError(t, os.MkdirAll(missingFileDir.String(), 0770))
132+
require.NoError(t, missingFileDir.MkdirAll())
133133

134134
defer oldDir1.RemoveAll()
135135
defer missingFileDir.RemoveAll()

0 commit comments

Comments
 (0)