From 775331ce692fe0d9e229524b8ad79cc5ad09619b Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 11:56:07 -0800 Subject: [PATCH] more cache --- src/components/cli/cli.go | 9 ++++++++- src/components/commands/nowPlaying.go | 25 +++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index c513b02..8930871 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -111,8 +111,15 @@ func Run(c *commands.Commander, s fx.Shutdowner) { Name: "nowplaying", Aliases: []string{"now"}, Usage: "Prints the current song", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "bypass cache", + }, + }, Action: func(cCtx *cli.Context) error { - return c.NowPlaying() + return c.NowPlaying(cCtx.Bool("force")) }, }, { diff --git a/src/components/commands/nowPlaying.go b/src/components/commands/nowPlaying.go index 9fb9947..44b68f6 100644 --- a/src/components/commands/nowPlaying.go +++ b/src/components/commands/nowPlaying.go @@ -2,17 +2,34 @@ package commands import ( "fmt" + "time" "github.com/zmb3/spotify/v2" ) -func (c *Commander) NowPlaying() error { - current, err := c.Client.PlayerCurrentlyPlaying(c.Context) +func (c *Commander) NowPlaying(force bool) error { + if force { + current, err := c.Client.PlayerCurrentlyPlaying(c.Context) + if err != nil { + return err + } + str := FormatSong(current) + fmt.Println(str) + _, err = c.Cache.Put("now_playing", str, 5*time.Second) + return err + } + song, err := c.Cache.GetOrDo("now_playing", func() (string, error) { + current, err := c.Client.PlayerCurrentlyPlaying(c.Context) + if err != nil { + return "", err + } + str := FormatSong(current) + return str, nil + }, 5*time.Second) if err != nil { return err } - str := FormatSong(current) - fmt.Println(str) + fmt.Println(song) return nil }