From 057b53113924aa1f0347b6db65a324b49af76551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sat, 22 Aug 2020 23:34:31 +0200 Subject: Pretty up the cache interface; ensure feed2imap-go unlocks the cache --- internal/feed/cache.go | 7 ++++--- internal/feed/state.go | 6 +++++- main.go | 1 + tools/print-cache/print-cache.go | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/feed/cache.go b/internal/feed/cache.go index bf64c4a..5f5c394 100644 --- a/internal/feed/cache.go +++ b/internal/feed/cache.go @@ -77,7 +77,7 @@ func lock(fileName string) (lock lockfile.Lockfile, err error) { return } -func storeCache(cache Cache, fileName string) error { +func (cache *Cache) store(fileName string) error { if cache.CacheImpl == nil { return fmt.Errorf("trying to store nil cache") } @@ -104,15 +104,16 @@ func storeCache(cache Cache, fileName string) error { writer.Flush() log.Printf("Stored cache to '%s'.", fileName) - return UnlockCache(cache) + return cache.Unlock() } -func UnlockCache(cache Cache) error { +func (cache *Cache) Unlock() error { if cache.locked { if err := cache.lock.Unlock(); err != nil { return fmt.Errorf("Unlocking cache: %w", err) } } + cache.locked = false return nil } diff --git a/internal/feed/state.go b/internal/feed/state.go index f2eff72..41d8751 100644 --- a/internal/feed/state.go +++ b/internal/feed/state.go @@ -63,7 +63,11 @@ func (state *State) LoadCache(fileName string, forceNew bool) error { } func (state *State) StoreCache(fileName string) error { - return storeCache(state.cache, fileName) + return state.cache.store(fileName) +} + +func (state *State) UnlockCache() { + _ = state.cache.Unlock() } func (state *State) Fetch() int { diff --git a/main.go b/main.go index 8d3aa15..99f104b 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,7 @@ func run() error { if err != nil { return err } + defer state.UnlockCache() state.RemoveUndue() diff --git a/tools/print-cache/print-cache.go b/tools/print-cache/print-cache.go index b21643e..cdecac4 100644 --- a/tools/print-cache/print-cache.go +++ b/tools/print-cache/print-cache.go @@ -27,7 +27,7 @@ func main() { log.Fatal(err) } - defer func() {_ = feed.UnlockCache(cache)}() + defer cache.Unlock() fmt.Printf("Cache version %d\n", cache.Version()) if feedId != "" { -- cgit v1.2.3