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

@@ -8,21 +8,20 @@ import (
func (c *Client) GetAnimeByID(ctx context.Context, id int) (Anime, error) {
cacheKey := fmt.Sprintf("anime:%d", id)
var cached Anime
if c.getCache(ctx, cacheKey, &cached) {
return cached, nil
}
var stale Anime
hasStale := c.getStaleCache(ctx, cacheKey, &stale)
var result AnimeResponse
reqURL := fmt.Sprintf("%s/anime/%d/full", c.baseURL, id)
if err := c.fetchWithRetry(ctx, reqURL, &result); err != nil {
if hasStale {
var stale Anime
if c.getStaleCache(ctx, cacheKey, &stale) {
return stale, nil
}
return Anime{}, err
}
@@ -33,4 +32,4 @@ func (c *Client) GetAnimeByID(ctx context.Context, id int) (Anime, error) {
c.setCache(ctx, cacheKey, result.Data, ttl)
return result.Data, nil
}
}