aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/cache
diff options
context:
space:
mode:
Diffstat (limited to 'internal/feed/cache')
-rw-r--r--internal/feed/cache/cache.go16
-rw-r--r--internal/feed/cache/v1.go4
-rw-r--r--internal/feed/cache/v2.go63
3 files changed, 76 insertions, 7 deletions
diff --git a/internal/feed/cache/cache.go b/internal/feed/cache/cache.go
index 9613f06..1ea8eaf 100644
--- a/internal/feed/cache/cache.go
+++ b/internal/feed/cache/cache.go
@@ -18,7 +18,7 @@ import (
type Version byte
const (
- currentVersion Version = v1Version
+ currentVersion Version = v2Version
)
type Impl interface {
@@ -56,6 +56,8 @@ func forVersion(version Version) (Impl, error) {
switch version {
case v1Version:
return newV1Cache(), nil
+ case v2Version:
+ return newV2Cache(), nil
default:
return nil, fmt.Errorf("unknown cache version '%d'", version)
}
@@ -169,11 +171,15 @@ func Load(fileName string) (Cache, error) {
return Cache{}, fmt.Errorf("decoding for version '%d' from '%s': %w", version, fileName, err)
}
- if cache, err = cache.transformTo(currentVersion); err != nil {
- return Cache{}, fmt.Errorf("cannot transform from version %d to %d: %w", version, currentVersion, err)
- }
+ if currentVersion != cache.Version() {
+ if cache, err = cache.transformTo(currentVersion); err != nil {
+ return Cache{}, fmt.Errorf("cannot transform from version %d to %d: %w", version, currentVersion, err)
+ }
- log.Printf("Loaded cache (version %d), transformed to version %d.", version, currentVersion)
+ log.Printf("Loaded cache (version %d), transformed to version %d.", version, currentVersion)
+ } else {
+ log.Printf("Loaded cache (version %d)", version)
+ }
return Cache{cache, lock, true}, nil
}
diff --git a/internal/feed/cache/v1.go b/internal/feed/cache/v1.go
index 17a0346..0363303 100644
--- a/internal/feed/cache/v1.go
+++ b/internal/feed/cache/v1.go
@@ -179,8 +179,8 @@ func newV1Cache() *v1Cache {
func (cache *v1Cache) transformTo(v Version) (Impl, error) {
switch v {
- case v1Version:
- return cache, nil
+ case v2Version:
+ return (*v2Cache)(cache), nil
default:
return nil, fmt.Errorf("Transformation not supported")
}
diff --git a/internal/feed/cache/v2.go b/internal/feed/cache/v2.go
new file mode 100644
index 0000000..87636b6
--- /dev/null
+++ b/internal/feed/cache/v2.go
@@ -0,0 +1,63 @@
+package cache
+
+import (
+ "compress/gzip"
+ "fmt"
+ "io"
+
+ "github.com/Necoro/feed2imap-go/internal/feed"
+)
+
+const v2Version Version = 2
+
+// v2Cache is identical to v1Cache, but uses gzip compression for storage
+type v2Cache v1Cache
+
+func newV2Cache() *v2Cache {
+ return (*v2Cache)(newV1Cache())
+}
+
+func (cache *v2Cache) asV1() *v1Cache {
+ return (*v1Cache)(cache)
+}
+
+func (cache *v2Cache) cachedFeed(feed *feed.Feed) CachedFeed {
+ return cache.asV1().cachedFeed(feed)
+}
+
+func (cache *v2Cache) transformTo(v Version) (Impl, error) {
+ return nil, fmt.Errorf("Transformation not supported")
+}
+
+func (cache *v2Cache) Version() Version {
+ return v2Version
+}
+
+func (cache *v2Cache) Info() string {
+ return cache.asV1().Info()
+}
+
+func (cache *v2Cache) SpecificInfo(i interface{}) string {
+ return cache.asV1().SpecificInfo(i)
+}
+
+func (cache *v2Cache) load(reader io.Reader) error {
+ gzipReader, err := gzip.NewReader(reader)
+ if err != nil {
+ return err
+ }
+ defer gzipReader.Close()
+
+ return cache.asV1().load(gzipReader)
+}
+
+func (cache *v2Cache) store(writer io.Writer) error {
+ gzipWriter := gzip.NewWriter(writer)
+ defer gzipWriter.Close()
+
+ if err := cache.asV1().store(gzipWriter); err != nil {
+ return err
+ }
+
+ return gzipWriter.Flush()
+}
d2imap-go.git/commit/pkg/version/version.go?h=v0.5.1&id=47b2f99d09a0dd30ecceb2190773bb6cc337f6d2&follow=1'>Release v0.2.0v0.2.0René 'Necoro' Neumann2-2/+6 2020-05-10Fix building cacheRené 'Necoro' Neumann1-1/+3 2020-05-10Update READMERené 'Necoro' Neumann1-5/+44 2020-05-10Ignore 'dist' folder and build productsRené 'Necoro' Neumann1-0/+2 2020-05-08Print item hashes in debug modeRené 'Necoro' Neumann1-1/+7 2020-05-07Improve html renderingRené 'Necoro' Neumann2-53/+32 2020-05-07Do not assume items to be new when their published date is newer than the las...René 'Necoro' Neumann2-7/+1 2020-05-07Updating some depsRené 'Necoro' Neumann2-2/+7 2020-05-07Better detection if a text starts with html or notRené 'Necoro' Neumann2-4/+13 2020-05-07go fmtRené 'Necoro' Neumann1-3/+2 2020-05-07Add header X-Feed2Imap-GUIDRené 'Necoro' Neumann3-1/+7 2020-05-07update changelogRené 'Necoro' Neumann1-0/+1 2020-05-07FixRené 'Necoro' Neumann1-1/+1 2020-05-07Unified publishedDate and updatedDate into one (just as the old feed2imap...)René 'Necoro' Neumann5-21/+32 2020-05-06Print version during startupRené 'Necoro' Neumann1-1/+1 2020-05-06Improve templateRené 'Necoro' Neumann3-20/+28 2020-05-05Fix pipelineRené 'Necoro' Neumann1-2/+5 2020-05-05Make changelog a part of the release pipeline (untested)René 'Necoro' Neumann2-0/+12