Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7efac1

Browse files
author
Luca Bianconi
committed
refactor: new interface
1 parent 3b11d67 commit d7efac1

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

‎buildcache/build_cache.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,31 @@ import (
2525

2626
const 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+
5868
func removeIfExpired(dir *paths.Path, ttl time.Duration) {
5969
fileInfo, err := dir.Join().Stat()
6070
if err != nil {

‎buildcache/build_cache_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) {
4343
}
4444

4545
func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
46-
_, err := GetOrCreate(dir)
46+
_, err := New(dir.Parent()).GetOrCreate(dir.Base())
4747
require.NoError(t, err)
4848
expectedFile := dir.Join(lastUsedFileName)
4949
fileInfo, err := expectedFile.Stat()
@@ -71,7 +71,7 @@ func TestPurge(t *testing.T) {
7171
require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime))
7272
}
7373

74-
Purge(dirToPurge, ttl)
74+
New(dirToPurge).Purge(ttl)
7575

7676
files, err := dirToPurge.Join("fresh").Stat()
7777
require.Nil(t, err)

‎commands/compile/compile.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
138138
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
139139
}
140140

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

@@ -312,6 +312,6 @@ func maybePurgeBuildCache() {
312312
}
313313
inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0)
314314
cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs()
315-
buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL)
316-
buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL)
315+
buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL)
316+
buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL)
317317
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /