From 459b6b2f02ee07ad72cc7ad21d9ee0ab1ed88140 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 14:37:07 +0530 Subject: [PATCH] Added Caching Module The Caching Module Caches the images that have been extracted and for persistence writes the images to a cache file. In the cache file the data is stored by tab separated values `%s\t%s\t%s` the cache is first loaded in the memory ( CACHE_LIST ) during the start of application and then extracted images are added to the map CACHE_LIST which is writtern to the cache file before exiting the program. --- cache/cache.go | 41 ++++++++++++++++++++++++++++++++++++++++ cache/cache_test.go | 16 ++++++++++++++++ cache/testdata/cache.txt | 2 ++ 3 files changed, 59 insertions(+) create mode 100644 cache/cache.go create mode 100644 cache/cache_test.go create mode 100644 cache/testdata/cache.txt diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..4c79390 --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,41 @@ +package cache + +import ( + "fmt" + "io/ioutil" + "os" + "strings" +) + +var ( + CACHE_LIST map[[2]string]string = make(map[[2]string]string) +) + +func loadCache(path string) { + cacheFileContent, err := ioutil.ReadFile(path) + if err != nil { + fmt.Println("Could Not Read From Cache File") + } + lineSlice := strings.Split(string(cacheFileContent), "\n") + for _, line := range lineSlice { + if len(line) != 0 { + param := strings.Split(line, "\t") + if len(param) == 3 { + CACHE_LIST[[2]string{param[0], param[1]}] = param[2] + } + } + } +} + +func writeToCache(path string) { + b, err := os.Create(path) + if err == nil { + for k, v := range CACHE_LIST { + b.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", k[0], k[1], v))) + } + } +} + +func generateName(artist, album string) string { + return fmt.Sprintf("%s-%s.jpg", artist, album) +} diff --git a/cache/cache_test.go b/cache/cache_test.go new file mode 100644 index 0000000..a03ef57 --- /dev/null +++ b/cache/cache_test.go @@ -0,0 +1,16 @@ +package cache + +import "testing" + +func TestLoadCache(t *testing.T) { + expectedResult := [2]string{"hello/wer.jpg", "hello/iwer.jpg"} + loadCache("./testdata/cache.txt") + var i int = 0 + for _, v := range CACHE_LIST { + if v != expectedResult[i] { + if v != expectedResult[i+1] { + t.Errorf("Didn't Get The Expected Value receieved %s", v) + } + } + } +} diff --git a/cache/testdata/cache.txt b/cache/testdata/cache.txt new file mode 100644 index 0000000..1366cb6 --- /dev/null +++ b/cache/testdata/cache.txt @@ -0,0 +1,2 @@ +Kanye West Life Of Pablo hello/wer.jpg +Niravana Heart Shaped Box hello/iwer.jpg