diff --git a/lib/cache.go b/lib/cache.go index 4df891e..a8edbc8 100644 --- a/lib/cache.go +++ b/lib/cache.go @@ -1,11 +1,12 @@ package lib import ( - "encoding/json" + "encoding/json" // Added for logging "os" + // Added for path manipulation ) -const cacheFile = "cache.json" +const cacheFileName = "cache.json" // Renamed for clarity type CacheItem struct { Fetcher string @@ -13,7 +14,15 @@ type CacheItem struct { } type Cache struct { - Items []CacheItem + Items []CacheItem + cacheFilePath string // New field to store the full path +} + +func NewCache() *Cache { + return &Cache{ + Items: make([]CacheItem, 0), + cacheFilePath: cacheFileName, + } } func (c *Cache) Update(fetcher string, value string) { @@ -45,7 +54,10 @@ func (c *Cache) Has(fetcher string) bool { func (c *Cache) IsChanged(fetcher string, value string) bool { cachedValue, found := c.Get(fetcher) - if !found || cachedValue != value { + if !found { + return true + } + if cachedValue != value { return true } return false @@ -60,21 +72,21 @@ func (c *Cache) TryUpdate(fetcher string, value string) bool { } func (c *Cache) Load() { - f, err := os.Open(cacheFile) + f, err := os.Open(c.cacheFilePath) if err != nil { return } defer f.Close() - _ = json.NewDecoder(f).Decode(&c.Items) + json.NewDecoder(f).Decode(&c.Items) } func (c *Cache) Save() { - f, err := os.Create(cacheFile) + f, err := os.Create(c.cacheFilePath) if err != nil { return } defer f.Close() - _ = json.NewEncoder(f).Encode(c.Items) + json.NewEncoder(f).Encode(c.Items) } diff --git a/lib/runner.go b/lib/runner.go index 8028df9..cc3bf06 100644 --- a/lib/runner.go +++ b/lib/runner.go @@ -47,7 +47,7 @@ func getFetchers(config Config, cache *Cache) []Fetcher { } func getCache() Cache { - cache := Cache{} + cache := *NewCache() cache.Load() return cache }