Skip to content

Commit

Permalink
feat: periodic updates
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jul 19, 2024
1 parent a025c0b commit 02e183e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"net/http"
"time"

"github.com/caarlos0/env/v11"
"github.com/gleich/lcp-v2/pkg/apis/github"
Expand Down Expand Up @@ -46,6 +47,7 @@ func main() {
githubCache := cache.New("github", github.FetchPinnedRepos(githubClient))
r.Get("/github/cache", githubCache.Route())
lumber.Success("init github cache")
go githubCache.PeriodicUpdate(func() []github.Repository { return github.FetchPinnedRepos(githubClient) }, 5*time.Minute)

stravaTokens := strava.LoadTokens()
stravaTokens.RefreshIfNeeded()
Expand All @@ -67,6 +69,7 @@ func main() {
steamCache := cache.New("steam", games)
r.Get("/steam/cache", steamCache.Route())
lumber.Success("init steam cache")
go steamCache.PeriodicUpdate(steam.FetchRecentlyPlayedGames, 5*time.Minute)

err = http.ListenAndServe(":8000", r)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ func (c *Cache[T]) Update(data T) {
lumber.Success(c.Name, "updated")
}
}

func (c *Cache[T]) PeriodicUpdate(updateFunc func() T, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
c.Update(updateFunc())
}
}

0 comments on commit 02e183e

Please sign in to comment.