Caching & freshness

The SDK is the cache. How long results are reused, what stale and fromCache mean, and how rate limits and outages are absorbed.

On this page

Sponsorkit has no database, so the SDK is the cache. Each result is stored on the supporter's device and served again, without any network call, for cacheTtl seconds. A cache miss is a live query to GitHub or Open Collective with the supporter's own permission, which typically takes 0.3 to 1 second.

Two reasons to cache: your app stays fast and works offline, and the providers aren't asked the same question on every run.

Setting the TTL#

The default is one hour. A value below 60 seconds is raised to 60. A single call can ask for fresher data, down to 0, which always asks the API:

const sk = new Sponsorkit({ github: "octocat", cacheTtl: 6 * 3600 }); // seconds, minimum 60

await sk.getFinancialContribution();                               // cached for up to 6 hours
const fresh = await sk.getFinancialContribution({ cacheTtl: 0 }); // ask the API now

Choosing a value:

  • CLIs that run many times a day are well served by the default: at most one request per hour.
  • Long-running apps can call getFinancialContribution() whenever they need the answer; it's a local read until the TTL passes.
  • Don't force 0 on every start. The API allows one refresh per minute per session; faster calls get the cache back (see below).

A new or cancelled sponsorship shows up within cacheTtl. After a checkout you don't have to wait: openCheckout() polls with a TTL of 0 until the new contribution appears; see the checkout guide.

fromCache and stale#

Two flags tell you where a result came from:

  • fromCache: true: the whole result was read from the local cache; no request was made, or the request failed.
  • stale: true: some data may be out of date, because a provider (or the whole API) couldn't be reached.
SituationfromCachestaleContributions
Fresh cache hittrueAs storedFrom the cache
Successful refreshfalsefalseLive
One provider unavailablefalsetrueLive, plus that provider's from the cache
Network error, 5xx or rate limited, with a cachetruetrueFrom the cache, whatever its age
Same, with nothing cachedfalsetrueNone: signedIn: true, supporter: null

You rarely need to act on these flags. They're useful for a "last checked" hint in a UI, or for being lenient: for example, keep showing a supporter badge while stale is true rather than taking it away during an outage.

Rate limits and Retry-After#

The API refreshes each session at most once per minute. A faster refresh gets 429 with a Retry-After header, and the SDK serves the cache. Until that time passes, the SDK doesn't call the API for that target set from the same process, even with a TTL of 0. Retry-After values are capped at 24 hours.

Provider quotas are per supporter (GitHub allows 5,000 requests an hour and Open Collective 100 a minute), so normal use is far below them.

When the cache is written and cleared#

  • Sign-in stores its first result as the cache, so the next check is instant.
  • Every successful refresh replaces the cache, and stores a renewed session token if the API sent one.
  • Concurrent processes don't undo each other: a refresh writes back only if the stored session token is still the one it used, so a sign-out or new sign-in in another process wins.
  • Sign-out and a rejected (session_invalid) token delete the token and the cache. Nag counters are kept.
  • Mock and disabled modes never touch the cache or the disk.

Where the cache lives on each platform is listed under supported platforms.