This commit is contained in:
2026-01-19 23:48:27 +01:00
parent cbd2695a81
commit 40030863c3
2 changed files with 21 additions and 9 deletions

View File

@@ -1,11 +1,12 @@
package lib package lib
import ( import (
"encoding/json" "encoding/json" // Added for logging
"os" "os"
// Added for path manipulation
) )
const cacheFile = "cache.json" const cacheFileName = "cache.json" // Renamed for clarity
type CacheItem struct { type CacheItem struct {
Fetcher string Fetcher string
@@ -13,7 +14,15 @@ type CacheItem struct {
} }
type Cache 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) { 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 { func (c *Cache) IsChanged(fetcher string, value string) bool {
cachedValue, found := c.Get(fetcher) cachedValue, found := c.Get(fetcher)
if !found || cachedValue != value { if !found {
return true
}
if cachedValue != value {
return true return true
} }
return false return false
@@ -60,21 +72,21 @@ func (c *Cache) TryUpdate(fetcher string, value string) bool {
} }
func (c *Cache) Load() { func (c *Cache) Load() {
f, err := os.Open(cacheFile) f, err := os.Open(c.cacheFilePath)
if err != nil { if err != nil {
return return
} }
defer f.Close() defer f.Close()
_ = json.NewDecoder(f).Decode(&c.Items) json.NewDecoder(f).Decode(&c.Items)
} }
func (c *Cache) Save() { func (c *Cache) Save() {
f, err := os.Create(cacheFile) f, err := os.Create(c.cacheFilePath)
if err != nil { if err != nil {
return return
} }
defer f.Close() defer f.Close()
_ = json.NewEncoder(f).Encode(c.Items) json.NewEncoder(f).Encode(c.Items)
} }

View File

@@ -47,7 +47,7 @@ func getFetchers(config Config, cache *Cache) []Fetcher {
} }
func getCache() Cache { func getCache() Cache {
cache := Cache{} cache := *NewCache()
cache.Load() cache.Load()
return cache return cache
} }