fixes
This commit is contained in:
28
lib/cache.go
28
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user