Skip to content

Commit fc7a398

Browse files
authored
[plugins] Add env var to control github cache ttl (#2314)
## Summary This adds new experimental env var `DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL` that controls how long devbox caches github plugins. The default is 24h. Ideally we store plugin versions in lockfile, but this can help improve UX in the meantime. ## How was it tested? Added print statement to monitor network requests ```bash DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=0 devbox run echo hello DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1s devbox run echo hello DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1h devbox run echo hello ```
1 parent 59c2d6d commit fc7a398

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

internal/plugin/github.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,28 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
6969
if err != nil {
7070
return nil, err
7171
}
72+
73+
// Cache for 24 hours. Once we store the plugin in the lockfile, we
74+
// should cache this indefinitely and only invalidate if the plugin
75+
// is updated.
76+
ttl := 24 * time.Hour
77+
78+
// This is a stopgap until plugin is stored in lockfile.
79+
// DEVBOX_X indicates this is an experimental env var.
80+
// Use DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL to override the default TTL.
81+
// e.g. DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1h will cache the plugin for 1 hour.
82+
// Note: If you want to disable cache, we recommend using a low second value instead of zero to
83+
// ensure only one network request is made.
84+
ttlStr := os.Getenv("DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL")
85+
if ttlStr != "" {
86+
ttl, err = time.ParseDuration(ttlStr)
87+
if err != nil {
88+
return nil, err
89+
}
90+
}
91+
7292
return githubCache.GetOrSet(
73-
contentURL,
93+
contentURL+ttlStr,
7494
func() ([]byte, time.Duration, error) {
7595
req, err := p.request(contentURL)
7696
if err != nil {
@@ -96,10 +116,8 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
96116
if err != nil {
97117
return nil, 0, err
98118
}
99-
// Cache for 24 hours. Once we store the plugin in the lockfile, we
100-
// should cache this indefinitely and only invalidate if the plugin
101-
// is updated.
102-
return body, 24 * time.Hour, nil
119+
120+
return body, ttl, nil
103121
},
104122
)
105123
}

0 commit comments

Comments
 (0)