perf: heavily optimize jikan cache by pre-warming individual anime objects and using 30-day TTLs for completed shows
This commit is contained in:
@@ -19,6 +19,11 @@ func (c *Client) GetAnimeByID(id int) (Anime, error) {
|
|||||||
return Anime{}, err
|
return Anime{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.setCache(cacheKey, result.Data, time.Hour*24)
|
ttl := time.Hour * 24
|
||||||
|
if result.Data.Status == "Finished Airing" {
|
||||||
|
ttl = time.Hour * 24 * 30
|
||||||
|
}
|
||||||
|
|
||||||
|
c.setCache(cacheKey, result.Data, ttl)
|
||||||
return result.Data, nil
|
return result.Data, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,21 @@ func (c *Client) setCache(key string, data interface{}, ttl time.Duration) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// preWarmAnimeCache extracts individual anime from list responses and caches them
|
||||||
|
func (c *Client) preWarmAnimeCache(animes []Anime) {
|
||||||
|
for _, a := range animes {
|
||||||
|
cacheKey := fmt.Sprintf("anime:%d", a.MalID)
|
||||||
|
|
||||||
|
// Smart TTL: Finished shows rarely change, cache for 30 days. Airing/Upcoming shows cache for 24 hours.
|
||||||
|
ttl := time.Hour * 24
|
||||||
|
if a.Status == "Finished Airing" {
|
||||||
|
ttl = time.Hour * 24 * 30
|
||||||
|
}
|
||||||
|
|
||||||
|
c.setCache(cacheKey, a, ttl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fetchWithRetry provides robust fetching respecting Jikan's strict 3 req/sec rate limit
|
// fetchWithRetry provides robust fetching respecting Jikan's strict 3 req/sec rate limit
|
||||||
func (c *Client) fetchWithRetry(urlStr string, out interface{}) error {
|
func (c *Client) fetchWithRetry(urlStr string, out interface{}) error {
|
||||||
maxRetries := 5
|
maxRetries := 5
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func (c *Client) Search(query string, page int) (SearchResult, error) {
|
|||||||
HasNextPage: result.Pagination.HasNextPage,
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.preWarmAnimeCache(result.Data)
|
||||||
c.setCache(cacheKey, res, time.Hour*1)
|
c.setCache(cacheKey, res, time.Hour*1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@@ -58,6 +59,7 @@ func (c *Client) GetTopAnime(page int) (TopAnimeResult, error) {
|
|||||||
HasNextPage: result.Pagination.HasNextPage,
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.preWarmAnimeCache(result.Data)
|
||||||
c.setCache(cacheKey, res, time.Hour*1)
|
c.setCache(cacheKey, res, time.Hour*1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ func (c *Client) GetSchedule(day string) (ScheduleResult, error) {
|
|||||||
HasNextPage: result.Pagination.HasNextPage,
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.preWarmAnimeCache(result.Data)
|
||||||
c.setCache(cacheKey, res, time.Hour*1)
|
c.setCache(cacheKey, res, time.Hour*1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@@ -76,6 +77,7 @@ func (c *Client) GetSeasonsNow(page int) (TopAnimeResult, error) {
|
|||||||
HasNextPage: result.Pagination.HasNextPage,
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.preWarmAnimeCache(result.Data)
|
||||||
c.setCache(cacheKey, res, time.Hour*1)
|
c.setCache(cacheKey, res, time.Hour*1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@@ -102,6 +104,7 @@ func (c *Client) GetSeasonsUpcoming(page int) (TopAnimeResult, error) {
|
|||||||
HasNextPage: result.Pagination.HasNextPage,
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.preWarmAnimeCache(result.Data)
|
||||||
c.setCache(cacheKey, res, time.Hour*1)
|
c.setCache(cacheKey, res, time.Hour*1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user