Skip to content

Commit

Permalink
feat: make the cache more generic (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored May 24, 2024
1 parent 5cb4681 commit 647321e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
23 changes: 8 additions & 15 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"errors"
"os"
"time"

"github.com/nobe4/gh-not/internal/notifications"
)

type ExpiringReadWriter interface {
Read() (notifications.Notifications, error)
Write(notifications.Notifications) error
Read(any) error
Write(any) error
Expired() (bool, error)
}

Expand All @@ -27,26 +25,21 @@ func NewFileCache(ttlInHours int, path string) *FileCache {
}
}

func (c *FileCache) Read() (notifications.Notifications, error) {
func (c *FileCache) Read(out any) error {
content, err := os.ReadFile(c.path)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
return nil
}
return nil, err
}

notifications := notifications.Notifications{}
if err := json.Unmarshal(content, &notifications); err != nil {
return nil, err
return err
}

return notifications, nil
return json.Unmarshal(content, out)
}

func (c *FileCache) Write(n notifications.Notifications) error {
marshalled, err := json.Marshal(n)
func (c *FileCache) Write(in any) error {
marshalled, err := json.Marshal(in)
if 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 @@ -47,8 +47,8 @@ func (c *Client) loadCache() (notifications.Notifications, bool, error) {
return nil, false, err
}

n, err := c.cache.Read()
if err != nil {
n := notifications.Notifications{}
if err := c.cache.Read(&n); err != nil {
return nil, expired, err
}

Expand Down

0 comments on commit 647321e

Please sign in to comment.