From 02d04e1387daea2daa65bd675da0be9b418bfcf8 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sat, 28 Sep 2024 19:11:04 +0200 Subject: [PATCH] feat(cache): move the expiration logic out (#179) --- internal/cache/cache.go | 32 +++++++++++++++----------------- internal/gh/gh.go | 4 ++-- internal/manager/manager.go | 10 ++++++---- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index d885883..1b4be4b 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -1,3 +1,10 @@ +/* +Package cache provides a simple file-based cache implementation that fullfills +the RefreshReadWriter interface. + +It writes and reads the cache to a file in JSON format, along with the last +refresh time. +*/ package cache import ( @@ -9,17 +16,15 @@ import ( "time" ) -type ExpiringReadWriter interface { +type RefreshReadWriter interface { Read(any) error Write(any) error - Expired() bool + Refresh(time.Time) RefreshedAt() time.Time - SetRefreshedAt(time.Time) } type FileCache struct { path string - ttl time.Duration wrap *CacheWrap } @@ -28,11 +33,8 @@ type CacheWrap struct { RefreshedAt time.Time `json:"refreshed_at"` } -func NewFileCache(ttlInHours int, path string) *FileCache { - return &FileCache{ - path: path, - ttl: time.Duration(ttlInHours) * time.Hour, - } +func NewFileCache(path string) *FileCache { + return &FileCache{path: path} } func (c *FileCache) Read(out any) error { @@ -61,20 +63,16 @@ func (c *FileCache) Read(out any) error { return err } -func (c *FileCache) RefreshedAt() time.Time { - return c.wrap.RefreshedAt -} - -func (c *FileCache) SetRefreshedAt(t time.Time) { +func (c *FileCache) Refresh(t time.Time) { c.wrap.RefreshedAt = t } -func (c *FileCache) Expired() bool { - return time.Now().After(c.RefreshedAt().Add(c.ttl)) +func (c *FileCache) RefreshedAt() time.Time { + return c.wrap.RefreshedAt } func (c *FileCache) deprecatedRead(content []byte) error { - slog.Warn("Cache is in an deprecated format. Attempting to read from the old format.") + slog.Warn("Cache is in an format deprecated in v0.5.0. Attempting to read from the old format.") if err := json.Unmarshal(content, c.wrap.Data); err != nil { return err diff --git a/internal/gh/gh.go b/internal/gh/gh.go index f97d26b..a644edd 100644 --- a/internal/gh/gh.go +++ b/internal/gh/gh.go @@ -30,13 +30,13 @@ var ( type Client struct { API api.Requestor - cache cache.ExpiringReadWriter + cache cache.RefreshReadWriter maxRetry int maxPage int url string } -func NewClient(api api.Requestor, cache cache.ExpiringReadWriter, config config.Endpoint) *Client { +func NewClient(api api.Requestor, cache cache.RefreshReadWriter, config config.Endpoint) *Client { url := DefaultUrl query := url.Query() diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 8d1dd21..b66c3a8 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -16,7 +16,7 @@ import ( type Manager struct { Notifications notifications.Notifications - Cache cache.ExpiringReadWriter + Cache cache.RefreshReadWriter config *config.Data client *gh.Client Actions actions.ActionsMap @@ -29,7 +29,7 @@ func New(config *config.Data) *Manager { m := &Manager{} m.config = config - m.Cache = cache.NewFileCache(m.config.Cache.TTLInHours, m.config.Cache.Path) + m.Cache = cache.NewFileCache(m.config.Cache.Path) return m } @@ -50,7 +50,9 @@ func (m *Manager) Load() error { } func (m *Manager) Refresh() error { - if m.RefreshStrategy.ShouldRefresh(m.Cache.Expired()) { + expired := time.Now().After(m.Cache.RefreshedAt().Add(time.Duration(m.config.Cache.TTLInHours) * time.Hour)) + + if m.RefreshStrategy.ShouldRefresh(expired) { return m.refreshNotifications() } @@ -75,7 +77,7 @@ func (m *Manager) refreshNotifications() error { m.Notifications = m.Notifications.Uniq() m.Notifications, err = m.Enrich(m.Notifications) - m.Cache.SetRefreshedAt(time.Now()) + m.Cache.Refresh(time.Now()) return err }