refactor(jikan): add getWithCache helper and reduce duplication

This commit is contained in:
2026-04-20 01:42:06 +02:00
parent be5824ab5f
commit 5ddfc72f37
7 changed files with 67 additions and 133 deletions

View File

@@ -12,24 +12,9 @@ func (c *Client) GetEpisodes(ctx context.Context, animeID int, page int) (Episod
}
cacheKey := fmt.Sprintf("anime:%d:episodes:%d", animeID, page)
var cached EpisodesResponse
if c.getCache(ctx, cacheKey, &cached) {
return cached, nil
}
var stale EpisodesResponse
hasStale := c.getStaleCache(ctx, cacheKey, &stale)
var result EpisodesResponse
reqURL := fmt.Sprintf("%s/anime/%d/episodes?page=%d", c.baseURL, animeID, page)
if err := c.fetchWithRetry(ctx, reqURL, &result); err != nil {
if hasStale {
return stale, nil
}
return EpisodesResponse{}, err
}
c.setCache(ctx, cacheKey, result, 12*time.Hour)
return result, nil
}
err := c.getWithCache(ctx, cacheKey, 12*time.Hour, reqURL, &result)
return result, err
}