Simple Implementation of caching

Now before rendering the image the image is checked in the cache if it
exists then the image is rendered else it is added to the cache and then
rendered.
This commit is contained in:
aditya-K2 2021-11-20 21:47:43 +05:30
parent 68ef09544c
commit b91120252b

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/aditya-K2/goMP/cache"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.com/diamondburned/ueberzug-go" "gitlab.com/diamondburned/ueberzug-go"
) )
@ -43,18 +44,24 @@ func openImage(path string, c chan string) {
fw, fh := getFontWidth() fw, fh := getFontWidth()
var im *ueberzug.Image var im *ueberzug.Image
if path != "stop" { if path != "stop" {
absPath := viper.GetString("MUSIC_DIRECTORY") + path
extractedImage := extractImageFromFile(absPath)
if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" {
a, err := CONN.ListInfo(path) a, err := CONN.ListInfo(path)
var extractedImage string
if err == nil && len(a) != 0 { if err == nil && len(a) != 0 {
downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"]) if val, err := cache.GetFromCache(a[0]["artist"], a[0]["album"]); err == nil {
extractedImage = val
} else {
imagePath := cache.AddToCache(a[0]["artist"], a[0]["album"])
absPath := viper.GetString("MUSIC_DIRECTORY") + path
extractedImage = extractImageFromFile(absPath, imagePath)
if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" {
downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath)
if err == nil { if err == nil {
NOTIFICATION_SERVER.Send("Image From LastFM") NOTIFICATION_SERVER.Send("Image From LastFM")
extractedImage = downloadedImage extractedImage = downloadedImage
} }
} }
} }
}
img2, _ := getImg(extractedImage) img2, _ := getImg(extractedImage)
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y")) im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
} }