@@ -32,6 +32,7 @@ import (
32
32
"github.com/arduino/arduino-cli/arduino/discovery"
33
33
"github.com/arduino/arduino-cli/arduino/httpclient"
34
34
"github.com/arduino/arduino-cli/commands"
35
+ "github.com/arduino/arduino-cli/inventory"
35
36
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
36
37
"github.com/pkg/errors"
37
38
"github.com/sirupsen/logrus"
50
51
validVidPid = regexp .MustCompile (`0[xX][a-fA-F\d]{4}` )
51
52
)
52
53
54
+ func cachedApiByVidPid (vid , pid string ) ([]* rpc.BoardListItem , error ) {
55
+ var resp []* rpc.BoardListItem
56
+
57
+ cacheKey := fmt .Sprintf ("cache.builder-api.v3/boards/byvid/pid/%s/%s" , vid , pid )
58
+ if cachedResp := inventory .Store .GetString (cacheKey + ".data" ); cachedResp != "" {
59
+ ts := inventory .Store .GetTime (cacheKey + ".ts" )
60
+ if time .Since (ts ) < time .Hour * 24 {
61
+ // Use cached response
62
+ if cachedResp == "ErrNotFound" {
63
+ return nil , ErrNotFound
64
+ }
65
+ if err := json .Unmarshal ([]byte (cachedResp ), & resp ); err == nil {
66
+ return resp , nil
67
+ }
68
+ }
69
+ }
70
+
71
+ resp , err := apiByVidPid (vid , pid ) // Perform API requrest
72
+
73
+ if err == ErrNotFound {
74
+ inventory .Store .Set (cacheKey + ".data" , "ErrNotFound" )
75
+ inventory .Store .Set (cacheKey + ".ts" , time .Now ())
76
+ inventory .WriteStore ()
77
+ } else if err == nil {
78
+ if cachedResp , err := json .Marshal (resp ); err == nil {
79
+ inventory .Store .Set (cacheKey + ".data" , string (cachedResp ))
80
+ inventory .Store .Set (cacheKey + ".ts" , time .Now ())
81
+ inventory .WriteStore ()
82
+ }
83
+ }
84
+ return resp , err
85
+ }
86
+
53
87
func apiByVidPid (vid , pid string ) ([]* rpc.BoardListItem , error ) {
54
88
// ensure vid and pid are valid before hitting the API
55
89
if ! validVidPid .MatchString (vid ) {
@@ -116,7 +150,7 @@ func identifyViaCloudAPI(port *discovery.Port) ([]*rpc.BoardListItem, error) {
116
150
}
117
151
118
152
logrus .Debug ("Querying builder API for board identification..." )
119
- return apiByVidPid (id .Get ("vid" ), id .Get ("pid" ))
153
+ return cachedApiByVidPid (id .Get ("vid" ), id .Get ("pid" ))
120
154
}
121
155
122
156
// identify returns a list of boards checking first the installed platforms or the Cloud API
0 commit comments