Skip to content

Commit

Permalink
better caching, cache adjacent article also on app startup
Browse files Browse the repository at this point in the history
  • Loading branch information
piqoni committed Sep 25, 2024
1 parent 223613f commit bba828f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import (

var articleCache = make(map[string]string)

func fetchWebsiteWithCache(url string, updateTextChan chan<- string) {
func fetchWebsiteWithCache(url string, updateTextChan chan<- string, finally func()) {
// finally is used to warm up the cache for the adjacent article once first article is fetched
defer func() {
if finally != nil {
go finally()
}
}()
if text, ok := articleCache[url]; ok {
updateTextChan <- text
} else {
Expand Down Expand Up @@ -48,7 +54,10 @@ func main() {
SetDynamicColors(true).
SetScrollable(true)
textView.SetBorder(true).SetTitle(titles[0]).SetTitleAlign(0)
go fetchWebsite(urls[0], updateTextChan)

go fetchWebsiteWithCache(urls[0], updateTextChan, func() {
fetchWebsite(urls[1], nil) // warm up below article
})

flex := tview.NewFlex().
AddItem(list, 0, 1, true).
Expand All @@ -63,15 +72,15 @@ func main() {
textView.ScrollTo(offset, 0)

// Fetch current website
go fetchWebsiteWithCache(urls[index], updateTextChan)
go fetchWebsiteWithCache(urls[index], updateTextChan, nil)
textView.SetTitle(titles[index])

// Pre-fetch next and previous website if they exist
if index-1 >= 0 {
go fetchWebsite(urls[index-1], nil)
go fetchWebsiteWithCache(urls[index-1], nil, nil)
}
if index+1 < len(urls) {
go fetchWebsite(urls[index+1], nil)
go fetchWebsiteWithCache(urls[index+1], nil, nil)
}
}
})
Expand Down

0 comments on commit bba828f

Please sign in to comment.