Skip to content

Commit

Permalink
add cache for mal latest anime list
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 12, 2024
1 parent 57bf4ab commit 68b8419
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion integrations/myanimelist/anime_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"

"github.com/rs/zerolog/log"
"github.com/sonalys/animeman/internal/utils"
"github.com/sonalys/animeman/pkg/v1/animelist"
)
Expand All @@ -33,12 +34,17 @@ func (api *API) GetCurrentlyWatching(ctx context.Context) ([]animelist.Entry, er
req.URL.RawQuery = v.Encode()
resp, err := api.client.Do(req)
if err != nil {
if len(api.cachedAnimeList) > 0 {
log.Warn().Msgf("failed to fetch anime list, using cache: %s", err)
return api.cachedAnimeList, nil
}
return nil, fmt.Errorf("fetching response: %w", err)
}
defer resp.Body.Close()
var entries []AnimeListEntry
if err := json.NewDecoder(resp.Body).Decode(&entries); err != nil {
return nil, fmt.Errorf("reading response: %w", err)
}
return convertEntry(entries), nil
api.cachedAnimeList = convertEntry(entries)
return api.cachedAnimeList, nil
}
6 changes: 4 additions & 2 deletions integrations/myanimelist/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/sonalys/animeman/internal/roundtripper"
"github.com/sonalys/animeman/pkg/v1/animelist"
"golang.org/x/time/rate"
)

Expand All @@ -13,8 +14,9 @@ const userAgent = "github.com/sonalys/animeman"

type (
API struct {
Username string
client *http.Client
Username string
client *http.Client
cachedAnimeList []animelist.Entry
}
)

Expand Down

0 comments on commit 68b8419

Please sign in to comment.