more cache

This commit is contained in:
abs3nt 2024-02-18 11:56:07 -08:00
parent aaa467eee0
commit 775331ce69
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
2 changed files with 29 additions and 5 deletions

View File

@ -111,8 +111,15 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
Name: "nowplaying", Name: "nowplaying",
Aliases: []string{"now"}, Aliases: []string{"now"},
Usage: "Prints the current song", Usage: "Prints the current song",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "bypass cache",
},
},
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
return c.NowPlaying() return c.NowPlaying(cCtx.Bool("force"))
}, },
}, },
{ {

View File

@ -2,17 +2,34 @@ package commands
import ( import (
"fmt" "fmt"
"time"
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
func (c *Commander) NowPlaying() error { func (c *Commander) NowPlaying(force bool) error {
current, err := c.Client.PlayerCurrentlyPlaying(c.Context) 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 { if err != nil {
return err return err
} }
str := FormatSong(current) fmt.Println(song)
fmt.Println(str)
return nil return nil
} }