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

@@ -226,6 +226,32 @@ func (c *Client) setCache(parentCtx context.Context, key string, data any, ttl t
})
}
type cacheResult struct {
data any
hasStale bool
}
func (c *Client) getWithCache(ctx context.Context, cacheKey string, ttl time.Duration, url string, out any) error {
if c.getCache(ctx, cacheKey, out) {
return nil
}
var stale any
hasStale := c.getStaleCache(ctx, cacheKey, &stale)
if err := c.fetchWithRetry(ctx, url, out); err != nil {
if hasStale {
staleBytes, _ := json.Marshal(stale)
json.Unmarshal(staleBytes, out)
return nil
}
return err
}
c.setCache(ctx, cacheKey, out, ttl)
return nil
}
func (c *Client) fetchWithRetry(ctx context.Context, urlStr string, out any) error {
maxRetries := 5
for attempt := range maxRetries {