aboutsummaryrefslogtreecommitdiff
path: root/internal/cache/cache.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/cache/cache.go120
1 files changed, 0 insertions, 120 deletions
diff --git a/internal/cache/cache.go b/internal/cache/cache.go
deleted file mode 100644
index 979d661..0000000
--- a/internal/cache/cache.go
+++ /dev/null
@@ -1,120 +0,0 @@
-package cache
-
-import (
- "bufio"
- "encoding/gob"
- "errors"
- "fmt"
- "os"
-
- "github.com/Necoro/feed2imap-go/internal/log"
-)
-
-const currentVersion byte = 1
-
-type Cache interface {
- Version() byte
- transformToCurrent() (Cache, error)
-}
-
-type feedId struct {
- Name string
- Url string
-}
-
-type v1Cache struct {
- version byte
- Ids map[feedId]uint64
- NextId uint64
-}
-
-func (cache *v1Cache) Version() byte {
- return cache.version
-}
-
-func New() Cache {
- cache := v1Cache{Ids: map[feedId]uint64{}}
- cache.version = currentVersion
- return &cache
-}
-
-func cacheForVersion(version byte) (Cache, error) {
- switch version {
- case 1:
- return New(), nil
- default:
- return nil, fmt.Errorf("unknown cache version '%d'", version)
- }
-}
-
-func (cache *v1Cache) transformToCurrent() (Cache, error) {
- return cache, nil
-}
-
-func Store(fileName string, cache Cache) error {
- if cache == nil {
- return fmt.Errorf("trying to store nil cache")
- }
- if cache.Version() != currentVersion {
- return fmt.Errorf("trying to store cache with unsupported version '%d' (current: '%d')", cache.Version(), currentVersion)
- }
-
- f, err := os.Create(fileName)
- if err != nil {
- return fmt.Errorf("trying to store cache to '%s': %w", fileName, err)
- }
- defer f.Close()
-
- writer := bufio.NewWriter(f)
- if err = writer.WriteByte(currentVersion); err != nil {
- return fmt.Errorf("writing to '%s': %w", fileName, err)
- }
-
- encoder := gob.NewEncoder(writer)
- if err = encoder.Encode(cache); err != nil {
- return fmt.Errorf("encoding cache: %w", err)
- }
-
- writer.Flush()
- log.Printf("Stored cache to '%s'.", fileName)
-
- return nil
-}
-
-func Read(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 nil, fmt.Errorf("opening cache at '%s': %w", fileName, err)
- }
- defer f.Close()
-
- log.Printf("Loading cache from '%s'", fileName)
-
- reader := bufio.NewReader(f)
- version, err := reader.ReadByte()
- if err != nil {
- return nil, fmt.Errorf("reading from '%s': %w", fileName, err)
- }
-
- cache, err := cacheForVersion(version)
- if err != nil {
- return nil, err
- }
-
- decoder := gob.NewDecoder(reader)
- if err = decoder.Decode(cache); err != nil {
- return nil, fmt.Errorf("decoding for version '%d' from '%s': %w", version, fileName, err)
- }
-
- if cache, err = cache.transformToCurrent(); err != nil {
- return nil, 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)
-
- return cache, nil
-}
>René '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