more cache
This commit is contained in:
parent
aaa467eee0
commit
775331ce69
@ -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"))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user