Skip to content

Commit

Permalink
feat(cache): move the expiration logic out (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Sep 28, 2024
1 parent 9f794c8 commit 02d04e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
32 changes: 15 additions & 17 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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()
}

Expand All @@ -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
}
Expand Down

0 comments on commit 02d04e1

Please sign in to comment.