From 42832491d097f96190807641fe3015e59e1fe5d9 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 20 Nov 2021 20:21:54 +0530 Subject: [PATCH] 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. --- cache/cache.go | 22 ++++++++++++++++++---- cache/cache_test.go | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 8f454be..4dd3e1d 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,7 @@ package cache import ( + "errors" "fmt" "io/ioutil" "os" @@ -11,10 +12,10 @@ var ( 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) 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") 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) if err == nil { for k, v := range CACHE_LIST { @@ -37,5 +51,5 @@ func WriteToCache(path 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) } diff --git a/cache/cache_test.go b/cache/cache_test.go index a03ef57..3c5ce02 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -4,7 +4,7 @@ import "testing" func TestLoadCache(t *testing.T) { expectedResult := [2]string{"hello/wer.jpg", "hello/iwer.jpg"} - loadCache("./testdata/cache.txt") + LoadCache("./testdata/cache.txt") var i int = 0 for _, v := range CACHE_LIST { if v != expectedResult[i] {