@@ -32,6 +32,7 @@ import (
3232 "github.com/arduino/arduino-cli/arduino/discovery"
3333 "github.com/arduino/arduino-cli/arduino/httpclient"
3434 "github.com/arduino/arduino-cli/commands"
35+ "github.com/arduino/arduino-cli/inventory"
3536 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3637 "github.com/pkg/errors"
3738 "github.com/sirupsen/logrus"
5051 validVidPid = regexp .MustCompile (`0[xX][a-fA-F\d]{4}` )
5152)
5253
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+
5387func apiByVidPid (vid , pid string ) ([]* rpc.BoardListItem , error ) {
5488 // ensure vid and pid are valid before hitting the API
5589 if ! validVidPid .MatchString (vid ) {
@@ -116,7 +150,7 @@ func identifyViaCloudAPI(port *discovery.Port) ([]*rpc.BoardListItem, error) {
116150 }
117151
118152 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" ))
120154}
121155
122156// identify returns a list of boards checking first the installed platforms or the Cloud API
0 commit comments