diff options
-rw-r--r-- | internal/feed/cache/cache.go | 10 | ||||
-rw-r--r-- | internal/feed/cache/v1.go | 12 |
2 files changed, 17 insertions, 5 deletions
diff --git a/internal/feed/cache/cache.go b/internal/feed/cache/cache.go index 3a80df1..9613f06 100644 --- a/internal/feed/cache/cache.go +++ b/internal/feed/cache/cache.go @@ -2,9 +2,9 @@ package cache import ( "bufio" - "encoding/gob" "errors" "fmt" + "io" "os" "path/filepath" "time" @@ -24,6 +24,8 @@ const ( type Impl interface { cachedFeed(*feed.Feed) CachedFeed transformTo(Version) (Impl, error) + load(io.Reader) error + store(io.Writer) error Version() Version Info() string SpecificInfo(interface{}) string @@ -103,8 +105,7 @@ func (cache *Cache) store(fileName string) error { return fmt.Errorf("writing to '%s': %w", fileName, err) } - encoder := gob.NewEncoder(writer) - if err = encoder.Encode(cache.Impl); err != nil { + if err = cache.Impl.store(writer); err != nil { return fmt.Errorf("encoding cache: %w", err) } @@ -164,8 +165,7 @@ func Load(fileName string) (Cache, error) { return Cache{}, err } - decoder := gob.NewDecoder(reader) - if err = decoder.Decode(cache); err != nil { + if err = cache.load(reader); err != nil { return Cache{}, fmt.Errorf("decoding for version '%d' from '%s': %w", version, fileName, err) } diff --git a/internal/feed/cache/v1.go b/internal/feed/cache/v1.go index 7d95d4c..17a0346 100644 --- a/internal/feed/cache/v1.go +++ b/internal/feed/cache/v1.go @@ -3,8 +3,10 @@ package cache import ( "crypto/sha256" "encoding/base64" + "encoding/gob" "encoding/hex" "fmt" + "io" "sort" "strconv" "strings" @@ -373,3 +375,13 @@ func filterItems(items []cachedItem) []cachedItem { return copiedItems } + +func (cache *v1Cache) load(reader io.Reader) error { + decoder := gob.NewDecoder(reader) + return decoder.Decode(cache) +} + +func (cache *v1Cache) store(writer io.Writer) error { + encoder := gob.NewEncoder(writer) + return encoder.Encode(cache) +} |