New Functions For the Caching Module

The Following New Functions have been added
    1. AddToCache Adds to the CACHE_LIST map
    2. GetFromCache Retrieves path from CACHE_LIST map
    3. Rename WriteToCache -> WriteCache()
    4. GenerateName now replaces spaces with underscores.
This commit is contained in:
aditya-K2 2021-11-20 20:21:54 +05:30
parent 51b63a19e5
commit 42832491d0
2 changed files with 19 additions and 5 deletions

22
cache/cache.go vendored
View File

@ -1,6 +1,7 @@
package cache package cache
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -11,10 +12,10 @@ var (
CACHE_LIST map[[2]string]string = make(map[[2]string]string) CACHE_LIST map[[2]string]string = make(map[[2]string]string)
) )
func LoadCache(path string) { func LoadCache(path string) error {
cacheFileContent, err := ioutil.ReadFile(path) cacheFileContent, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println("Could Not Read From Cache File") return errors.New("Could Not Read From Cache File")
} }
lineSlice := strings.Split(string(cacheFileContent), "\n") lineSlice := strings.Split(string(cacheFileContent), "\n")
for _, line := range lineSlice { for _, line := range lineSlice {
@ -25,9 +26,22 @@ func LoadCache(path string) {
} }
} }
} }
return nil
} }
func WriteToCache(path string) { func GetFromCache(artist, album string) (string, error) {
if val, ok := CACHE_LIST[[2]string{artist, album}]; ok {
return val, nil
} else {
return "", errors.New("Element Not In Cache")
}
}
func AddToCache(artist, album string) {
CACHE_LIST[[2]string{artist, album}] = GenerateName(artist, album)
}
func WriteCache(path string) {
b, err := os.Create(path) b, err := os.Create(path)
if err == nil { if err == nil {
for k, v := range CACHE_LIST { for k, v := range CACHE_LIST {
@ -37,5 +51,5 @@ func WriteToCache(path string) {
} }
func GenerateName(artist, album string) string { func GenerateName(artist, album string) string {
return fmt.Sprintf("%s-%s.jpg", artist, album) return strings.Replace(fmt.Sprintf("%s-%s.jpg", artist, album), " ", "_", -1)
} }

2
cache/cache_test.go vendored
View File

@ -4,7 +4,7 @@ import "testing"
func TestLoadCache(t *testing.T) { func TestLoadCache(t *testing.T) {
expectedResult := [2]string{"hello/wer.jpg", "hello/iwer.jpg"} expectedResult := [2]string{"hello/wer.jpg", "hello/iwer.jpg"}
loadCache("./testdata/cache.txt") LoadCache("./testdata/cache.txt")
var i int = 0 var i int = 0
for _, v := range CACHE_LIST { for _, v := range CACHE_LIST {
if v != expectedResult[i] { if v != expectedResult[i] {