aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/cache.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-25 17:00:57 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-25 17:00:57 +0200
commit573ce1982da2e754947453fdaf0d50204873acb4 (patch)
treef6528235dce77db514ce4442ee8817e993fdcc86 /internal/feed/cache.go
parentd21881150c09986571a563eaf30bc1687787e63f (diff)
downloadfeed2imap-go-573ce1982da2e754947453fdaf0d50204873acb4.tar.gz
feed2imap-go-573ce1982da2e754947453fdaf0d50204873acb4.tar.bz2
feed2imap-go-573ce1982da2e754947453fdaf0d50204873acb4.zip
Larger restructuring
Diffstat (limited to 'internal/feed/cache.go')
-rw-r--r--internal/feed/cache.go42
1 files changed, 14 insertions, 28 deletions
diff --git a/internal/feed/cache.go b/internal/feed/cache.go
index 4f27144..411ed47 100644
--- a/internal/feed/cache.go
+++ b/internal/feed/cache.go
@@ -9,7 +9,7 @@ import (
"os"
"time"
- "github.com/Necoro/feed2imap-go/internal/log"
+ "github.com/Necoro/feed2imap-go/pkg/log"
)
const (
@@ -77,7 +77,7 @@ func (cache *v1Cache) Version() byte {
return cache.version
}
-func New() Cache {
+func newCache() Cache {
cache := v1Cache{
Ids: map[feedDescriptor]feedId{},
Feeds: map[feedId]*cachedFeed{},
@@ -90,7 +90,7 @@ func New() Cache {
func cacheForVersion(version byte) (Cache, error) {
switch version {
case 1:
- return New(), nil
+ return newCache(), nil
default:
return nil, fmt.Errorf("unknown cache version '%d'", version)
}
@@ -114,20 +114,20 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed {
return feed.cached.(*cachedFeed)
}
- fId := feedDescriptor{Name: feed.Name, Url: feed.Url}
- id, ok := cache.Ids[fId]
+ fDescr := feed.descriptor()
+ id, ok := cache.Ids[fDescr]
if !ok {
var otherId feedDescriptor
changed := false
for otherId, id = range cache.Ids {
- if otherId.Name == fId.Name {
- log.Warnf("Feed %s seems to have changed URLs: New '%s', old '%s'. Updating.",
- fId.Name, fId.Url, otherId.Url)
+ if otherId.Name == fDescr.Name {
+ log.Warnf("Feed %s seems to have changed URLs: newCache '%s', old '%s'. Updating.",
+ fDescr.Name, fDescr.Url, otherId.Url)
changed = true
break
- } else if otherId.Url == fId.Url {
- log.Warnf("Feed with URL '%s' seems to have changed its name: New '%s', old '%s'. Updating",
- fId.Url, fId.Name, otherId.Name)
+ } else if otherId.Url == fDescr.Url {
+ log.Warnf("Feed with URL '%s' seems to have changed its name: newCache '%s', old '%s'. Updating",
+ fDescr.Url, fDescr.Name, otherId.Name)
changed = true
break
}
@@ -139,7 +139,7 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed {
cache.NextId++
}
- cache.Ids[fId] = id
+ cache.Ids[fDescr] = id
}
item := cache.getItem(id)
@@ -147,8 +147,7 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed {
return item
}
-func (feeds *Feeds) StoreCache(fileName string) error {
- cache := feeds.cache
+func storeCache(cache Cache, fileName string) error {
if cache == nil {
return fmt.Errorf("trying to store nil cache")
}
@@ -178,25 +177,12 @@ func (feeds *Feeds) StoreCache(fileName string) error {
return nil
}
-func (feeds *Feeds) LoadCache(fileName string) error {
- cache, err := loadCache(fileName)
- if err != nil {
- return err
- }
- feeds.cache = cache
-
- for _, feed := range feeds.feeds {
- feed.cached = cache.findItem(feed)
- }
- return nil
-}
-
func loadCache(fileName string) (Cache, error) {
f, err := os.Open(fileName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// no cache there yet -- make new
- return New(), nil
+ return newCache(), nil
}
return nil, fmt.Errorf("opening cache at '%s': %w", fileName, err)
}