feat: use cached anime data for recommendations to show english titles when possible
This commit is contained in:
@@ -50,9 +50,15 @@ func (c *Client) GetRecommendations(animeID int, limit int) ([]Anime, error) {
|
||||
for i := 0; i < max; i++ {
|
||||
rec := result.Data[i]
|
||||
|
||||
// Map the recommendation data directly into an Anime struct.
|
||||
// By doing this locally, we avoid N additional rate-limited API calls
|
||||
// which was destroying the Jikan limit buckets.
|
||||
// Try to see if we already have the full anime details in our local cache.
|
||||
// If we do, we can use it to get the English title without making an API call!
|
||||
var fullAnime Anime
|
||||
animeCacheKey := fmt.Sprintf("anime:%d", rec.Entry.MalID)
|
||||
|
||||
if c.getCache(animeCacheKey, &fullAnime) {
|
||||
animes = append(animes, fullAnime)
|
||||
} else {
|
||||
// Otherwise, map the basic recommendation data directly into an Anime struct.
|
||||
anime := Anime{
|
||||
MalID: rec.Entry.MalID,
|
||||
Title: rec.Entry.Title,
|
||||
@@ -71,9 +77,9 @@ func (c *Client) GetRecommendations(animeID int, limit int) ([]Anime, error) {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
animes = append(animes, anime)
|
||||
}
|
||||
}
|
||||
|
||||
c.setCache(cacheKey, animes, time.Hour*24)
|
||||
return animes, nil
|
||||
|
||||
Reference in New Issue
Block a user