Skip to content

Commit

Permalink
feat: caching invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Jun 3, 2024
1 parent 428803a commit 8ebffb1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (m Model) handleWindowSize(msg tea.WindowSizeMsg) Model {
m.table.SetHeight(height - lipgloss.Height(m.help.View(m.keys)) - lib.MainStyle.GetBorderBottomSize())

if !m.ready {
m.feeds = lib.GetAllContent(lib.UserConfig.Urls, true)
m.feeds = lib.GetAllContent(lib.UserConfig.Urls, lib.CheckCache())
m.viewport = viewport.New(width, height)

err := error(nil)
Expand Down
41 changes: 41 additions & 0 deletions lib/fetch.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package lib

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"time"

"github.com/adrg/xdg"
"github.com/mmcdole/gofeed"
Expand Down Expand Up @@ -128,6 +130,10 @@ func setupReader(url string, preferCache bool) *gofeed.Feed {

// GetAllContent fetches the content of all URLs and returns it as a slice of Feeds
func GetAllContent(urls []string, preferCache bool) Feeds {
if !preferCache {
WriteCacheTime()

Check failure on line 134 in lib/fetch.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
}

// Create a wait group to wait for all goroutines to finish
var wg sync.WaitGroup

Expand Down Expand Up @@ -164,3 +170,38 @@ func fetchContent(url string, preferCache bool, wg *sync.WaitGroup, ch chan<- Fe
// Send the response through the channel
ch <- posts
}

func CheckCache() bool {
fileStr := getStateFile("fetch.json")
if _, err := os.Stat(fileStr); os.IsNotExist(err) {
err := WriteCacheTime()
if err != nil {
log.Fatalf("could not write tracking file: %v", err)
}
}

file, err := os.ReadFile(fileStr)
if err != nil {
log.Fatalf("could not read tracking file: %v", err)
}

last := &time.Time{}
err = json.Unmarshal(file, last)
if err != nil {
log.Fatalf("could not unmarshal tracking file: %v", err)
}

if time.Since(*last) > 24*time.Hour {
return false
}

return true
}

func WriteCacheTime() error {
json, err := json.Marshal(time.Now())
if err != nil {
return err
}
return os.WriteFile(getStateFile("fetch.json"), json, 0644)
}
8 changes: 4 additions & 4 deletions lib/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (feeds Feeds) WriteTracking() error {
if err != nil {
return err
}
return os.WriteFile(getStateFile(), json, 0644)
return os.WriteFile(getStateFile("tracking.json"), json, 0644)
}

// ReadTracking reads the tracking state from a JSON file
func (feeds Feeds) ReadTracking() (Feeds, error) {
fileStr := getStateFile()
fileStr := getStateFile("tracking.json")
if _, err := os.Stat(fileStr); os.IsNotExist(err) {
err := feeds.WriteTracking()
if err != nil {
Expand All @@ -65,8 +65,8 @@ func (feeds Feeds) ReadTracking() (Feeds, error) {
return mergedFeed, nil
}

func getStateFile() string {
stateFile, err := xdg.StateFile("izrss/tracking.json")
func getStateFile(file string) string {
stateFile, err := xdg.StateFile("izrss/" + file)
if err != nil {
log.Fatalf("could not find state file: %v", err)
}
Expand Down

0 comments on commit 8ebffb1

Please sign in to comment.