Cache Scopes

Imagine your cache as a library. By default, any book (cached entry) is only accessible to one person at a time (request auth). But what if multiple people want the same read the same exact book? Instead of reserving many identical books, we have a shared section for popular books.

This shared section is what we call "scopes."

Scopes let you mark certain data as "shared," so different users can access it. It's a way to efficiently serve common data to multiple users.

By default, all cached entries are tied to request auth. However, we can opt-out of this behaviour by using scopes field.

The response is cached using the API Key operations in the "cache key", allowing any request with matching API key operations to access the cached entries.

import { config } from '@grafbase/sdk' export default config({ cache: { rules: [ { types: ['Post'], maxAge: 60, scopes: ['apikey'], }, ], }, })

Responses are cached using the header value in the "cache key"; Any request with valid auth and the same header value can access this cache.

import { config, g } from '@grafbase/sdk' export default config({ cache: { rules: [ { types: ['Post'], maxAge: 60, scopes: [{ header: 'value' }], }, ], }, })

Responses are cached using the claim value in the cache key. Any request with a valid JWT and matching claim value are granted access to the cached response.

import { config, g } from '@grafbase/sdk' export default config({ cache: { rules: [ { types: ['Post'], maxAge: 60, scopes: [{ claim: 'sub' }], }, ], }, })

Responses are cached using the configured public operations in the cache key. Any request for a public operation gets access to the cached response.

import { config, g } from '@grafbase/sdk' export default config({ cache: { rules: [ { types: ['Post'], maxAge: 60, scopes: ['public'], }, ], }, })
Was this page helpful?