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