fix(cache): only store template cache on primary and transient

This commit is contained in:
Jan De Dobbeleer 2023-05-08 13:15:59 +02:00
parent 27dc979954
commit b51d1de205
No known key found for this signature in database
GPG key ID: D9FE64756B9A61E6
2 changed files with 14 additions and 2 deletions

View file

@ -17,6 +17,7 @@ func New(flags *platform.Flags) *Engine {
env.Init()
cfg := LoadConfig(env)
env.Var = cfg.Var
flags.HasTransient = cfg.TransientPrompt != nil
ansiWriter := &ansi.Writer{
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),

View file

@ -67,6 +67,7 @@ type Flags struct {
Manual bool
Plain bool
Primary bool
HasTransient bool
PromptCount int
Cleared bool
Version string
@ -743,12 +744,22 @@ func (env *Shell) Cache() Cache {
return env.fileCache
}
func (env *Shell) Close() {
defer env.Trace(time.Now())
func (env *Shell) saveTemplateCache() {
// only store this when in a primary prompt
// and when we have a transient prompt in the config
canSave := env.CmdFlags.Primary && env.CmdFlags.HasTransient
if !canSave {
return
}
templateCache, err := json.Marshal(env.TemplateCache())
if err == nil {
env.fileCache.Set(TEMPLATECACHE, string(templateCache), 1440)
}
}
func (env *Shell) Close() {
defer env.Trace(time.Now())
env.saveTemplateCache()
env.fileCache.Close()
}