package services import ( "context" "github.com/treytartt/honeydue-api/internal/models" "github.com/treytartt/honeydue-api/internal/repositories" ) // cachedSubscriptionSettings fetches the singleton settings row, going // through Redis (30-min TTL) before falling back to Postgres. // // Hot read — touched on every CheckLimit, every GetSubscriptionStatus, // and every Stripe webhook. The row is admin-toggleable but writes are // rare; the cache cuts the per-request cost from ~250ms (transatlantic // Postgres roundtrip) to ~1ms (cluster-internal Redis). // // On a nil cache (tests, Redis-down), falls through to the repo directly // so the caller never sees a hard failure from caching. // // Admin writes invalidate via cache.InvalidateSubscriptionSettings. func cachedSubscriptionSettings( ctx context.Context, cache *CacheService, subRepo *repositories.SubscriptionRepository, ) (*models.SubscriptionSettings, error) { if cache != nil { var settings models.SubscriptionSettings if err := cache.GetCachedSubscriptionSettings(ctx, &settings); err == nil { return &settings, nil } } settings, err := subRepo.WithContext(ctx).GetSettings() if err != nil { return nil, err } if cache != nil { _ = cache.CacheSubscriptionSettings(ctx, settings) } return settings, nil }