Skip to content

Commit cbfe06c

Browse files
committed
Using build-cache path to store libs cache / added cache expire every day
1 parent 816b4ef commit cbfe06c

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

commands/instances.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateReque
9292
if err != nil {
9393
return nil, err
9494
}
95-
inst, err := instances.Create(dataDir, packagesDir, userPackagesDir, downloadsDir, userAgent, config)
95+
inst, err := instances.Create(dataDir, packagesDir, userPackagesDir, downloadsDir, s.settings.GetBuildCachePath(), userAgent, config)
9696
if err != nil {
9797
return nil, err
9898
}
@@ -409,7 +409,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
409409
}
410410
}
411411

412-
lm, libsLoadingWarnings := lmb.Build()
412+
lm, libsLoadingWarnings := lmb.Build(s.settings.GetBuildCachePath())
413413
_ = instances.SetLibraryManager(instance, lm) // should never fail
414414
for _, status := range libsLoadingWarnings {
415415
logrus.WithError(status.Err()).Warnf("Error loading library")

commands/internal/instances/instances.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func SetLibraryManager(inst *rpc.Instance, lm *librariesmanager.LibrariesManager
134134
}
135135

136136
// Create a new *rpc.Instance ready to be initialized
137-
func Create(dataDir, packagesDir, userPackagesDir, downloadsDir *paths.Path, extraUserAgent string, downloaderConfig downloader.Config) (*rpc.Instance, error) {
137+
func Create(dataDir, packagesDir, userPackagesDir, downloadsDir, cacheDir *paths.Path, extraUserAgent string, downloaderConfig downloader.Config) (*rpc.Instance, error) {
138138
// Create package manager
139139
userAgent := "arduino-cli/" + version.VersionInfo.VersionString
140140
if extraUserAgent != "" {
@@ -143,7 +143,7 @@ func Create(dataDir, packagesDir, userPackagesDir, downloadsDir *paths.Path, ext
143143
tempDir := dataDir.Join("tmp")
144144

145145
pm := packagemanager.NewBuilder(dataDir, packagesDir, userPackagesDir, downloadsDir, tempDir, userAgent, downloaderConfig).Build()
146-
lm, _ := librariesmanager.NewBuilder().Build()
146+
lm, _ := librariesmanager.NewBuilder().Build(cacheDir)
147147

148148
instance := &coreInstance{
149149
pm: pm,

internal/arduino/builder/internal/detector/detector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func LibrariesLoader(
605605
if useCachedLibrariesResolution {
606606
// Since we are using the cached libraries resolution
607607
// the library manager is not needed.
608-
lm, _ = librariesmanager.NewBuilder().Build()
608+
lm, _ = librariesmanager.NewBuilder().Build(nil)
609609
}
610610
if librariesManager == nil {
611611
lmb := librariesmanager.NewBuilder()
@@ -653,7 +653,7 @@ func LibrariesLoader(
653653
})
654654
}
655655

656-
newLm, libsLoadingWarnings := lmb.Build()
656+
newLm, libsLoadingWarnings := lmb.Build(nil)
657657
for _, status := range libsLoadingWarnings {
658658
// With the refactoring of the initialization step of the CLI we changed how
659659
// errors are returned when loading platforms and libraries, that meant returning a list of

internal/arduino/libraries/librariesmanager/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (lmi *Installer) InstallPrerequisiteCheck(name string, version *semver.Vers
6565
return nil, err
6666
}
6767

68-
lmi.RescanLibraries()
68+
lmi.RescanLibraries(nil) // TODO: Is this required??
6969

7070
libs := lmi.FindByReference(name, nil, installLocation)
7171
if len(libs) > 1 {

internal/arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
package librariesmanager
1717

1818
import (
19+
"crypto/md5"
20+
"encoding/hex"
1921
"errors"
2022
"fmt"
2123
"os"
2224
"slices"
2325
"strings"
2426
"sync"
27+
"time"
2528

2629
"github.com/arduino/arduino-cli/internal/arduino/cores"
2730
"github.com/arduino/arduino-cli/internal/arduino/libraries"
@@ -124,12 +127,12 @@ func (lm *LibrariesManager) NewInstaller() (*Installer, func()) {
124127
}
125128

126129
// Build builds a new LibrariesManager.
127-
func (lmb *Builder) Build() (*LibrariesManager, []*status.Status) {
130+
func (lmb *Builder) Build(cacheDir *paths.Path) (*LibrariesManager, []*status.Status) {
128131
var statuses []*status.Status
129132
res := &LibrariesManager{}
130133
for _, dir := range lmb.librariesDir {
131134
if !dir.scanned {
132-
if errs := lmb.loadLibrariesFromDir(dir); len(errs) > 0 {
135+
if errs := lmb.loadLibrariesFromDir(dir, cacheDir); len(errs) > 0 {
133136
statuses = append(statuses, errs...)
134137
}
135138
}
@@ -167,11 +170,11 @@ func (lmb *Builder) AddLibrariesDir(libDir LibrariesDir) {
167170
}
168171

169172
// RescanLibraries reload all installed libraries in the system.
170-
func (lmi *Installer) RescanLibraries() []*status.Status {
173+
func (lmi *Installer) RescanLibraries(cacheDir *paths.Path) []*status.Status {
171174
lmi.libraries = map[string]libraries.List{}
172175
statuses := []*status.Status{}
173176
for _, dir := range lmi.librariesDir {
174-
if errs := lmi.loadLibrariesFromDir(dir); len(errs) > 0 {
177+
if errs := lmi.loadLibrariesFromDir(dir, cacheDir); len(errs) > 0 {
175178
statuses = append(statuses, errs...)
176179
}
177180
}
@@ -194,15 +197,30 @@ func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLoc
194197
}
195198
}
196199

200+
func (lm *LibrariesManager) libCacheFile(librariesDir *LibrariesDir, cacheDir *paths.Path) *paths.Path {
201+
if cacheDir == nil {
202+
return nil
203+
}
204+
hash := md5.Sum([]byte(librariesDir.Path.String()))
205+
cache := cacheDir.Join("libs", "cache_"+hex.EncodeToString(hash[:]))
206+
if stat, err := cache.Stat(); err != nil {
207+
return cache
208+
} else if time.Since(stat.ModTime()) < time.Hour*24 {
209+
return cache
210+
}
211+
cache.Remove()
212+
return cache
213+
}
214+
197215
// loadLibrariesFromDir loads all libraries in the given directory. Returns
198216
// nil if the directory doesn't exists.
199-
func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*status.Status {
217+
func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir, cacheDir *paths.Path) []*status.Status {
200218
statuses := []*status.Status{}
201219

202220
librariesDir.scanned = true
203221

204222
var loadedLibs libraries.List
205-
if cacheFilePath := librariesDir.Path.Join("libraries-loader-cache"); cacheFilePath.Exist() {
223+
if cacheFilePath := lm.libCacheFile(librariesDir, cacheDir); cacheFilePath != nil && cacheFilePath.Exist() {
206224
logrus.WithField("file", cacheFilePath).Info("Using library cache")
207225

208226
// Load lib cache
@@ -214,6 +232,7 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
214232
defer cache.Close()
215233

216234
if err := loadedLibs.UnmarshalBinary(cache); err != nil {
235+
cacheFilePath.Remove()
217236
s := status.Newf(codes.FailedPrecondition, "reading lib cache %[1]s: %[2]s", cacheFilePath, err)
218237
return append(statuses, s)
219238
}
@@ -246,17 +265,20 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
246265
}
247266

248267
// Write lib cache
249-
cache, err := cacheFilePath.Create()
250-
if err != nil {
251-
s := status.Newf(codes.FailedPrecondition, "creating lib cache %[1]s: %[2]s", cacheFilePath, err)
252-
return append(statuses, s)
253-
}
254-
err = loadedLibs.MarshalBinary(cache)
255-
cache.Close()
256-
if err != nil {
257-
cacheFilePath.Remove()
258-
s := status.Newf(codes.FailedPrecondition, "writing lib cache %[1]s: %[2]s", cacheFilePath, err)
259-
return append(statuses, s)
268+
if cacheFilePath != nil {
269+
cacheFilePath.Parent().MkdirAll()
270+
cache, err := cacheFilePath.Create()
271+
if err != nil {
272+
s := status.Newf(codes.FailedPrecondition, "creating lib cache %[1]s: %[2]s", cacheFilePath, err)
273+
return append(statuses, s)
274+
}
275+
err = loadedLibs.MarshalBinary(cache)
276+
cache.Close()
277+
if err != nil {
278+
cacheFilePath.Remove()
279+
s := status.Newf(codes.FailedPrecondition, "writing lib cache %[1]s: %[2]s", cacheFilePath, err)
280+
return append(statuses, s)
281+
}
260282
}
261283
}
262284

internal/arduino/libraries/librariesmanager/librariesmanager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ func TestLibrariesBuilderScanCloneRescan(t *testing.T) {
2525
lmb := NewBuilder()
2626
lmb.libraries["testLibA"] = libraries.List{}
2727
lmb.libraries["testLibB"] = libraries.List{}
28-
lm, warns := lmb.Build()
28+
lm, warns := lmb.Build(nil)
2929
require.Empty(t, warns)
3030
require.Len(t, lm.libraries, 2)
3131

3232
// Cloning should keep existing libraries
33-
lm2, warns2 := lm.Clone().Build()
33+
lm2, warns2 := lm.Clone().Build(nil)
3434
require.Empty(t, warns2)
3535
require.Len(t, lm2.libraries, 2)
3636

3737
// Full rescan should update libs
3838
{
3939
lmi2, release := lm2.NewInstaller()
40-
lmi2.RescanLibraries()
40+
lmi2.RescanLibraries(nil)
4141
release()
4242
}
4343
require.Len(t, lm.libraries, 2) // Ensure deep-coping worked as expected...

0 commit comments

Comments
 (0)