-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic cache for graphQL querys (as opt-in per query) #5343
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import {debugLogRequestInfo, errorHandler} from '../../../private/node/api/graph | |
import {addPublicMetadata, runWithTimer} from '../metadata.js' | ||
import {retryAwareRequest} from '../../../private/node/api.js' | ||
import {requestIdsCollection} from '../../../private/node/request-ids.js' | ||
import {nonRandomUUID} from '../crypto.js' | ||
import {cacheRetrieveOrRepopulate, GraphQLRequestKey} from '../../../private/node/conf-store.js' | ||
import { | ||
GraphQLClient, | ||
rawRequest, | ||
|
@@ -23,12 +25,20 @@ export interface GraphQLVariables { | |
|
||
export type GraphQLResponse<T> = Awaited<ReturnType<typeof rawRequest<T>>> | ||
|
||
export interface CacheOptions { | ||
cacheTTL?: CacheTTL | ||
cacheExtraKey?: string | ||
} | ||
export type CacheTTL = '1h' | '6h' | '12h' | '1d' | '3d' | '7d' | '14d' | '30d' | ||
|
||
interface GraphQLRequestBaseOptions<TResult> { | ||
api: string | ||
url: string | ||
token?: string | ||
addedHeaders?: {[header: string]: string} | ||
responseOptions?: GraphQLResponseOptions<TResult> | ||
cacheTTL?: CacheTTL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. base on the
(we only use extra key & caching in general if TTL provided) |
||
cacheExtraKey?: string | ||
} | ||
|
||
type PerformGraphQLRequestOptions<TResult> = GraphQLRequestBaseOptions<TResult> & { | ||
|
@@ -88,19 +98,43 @@ async function performGraphQLRequest<TResult>(options: PerformGraphQLRequestOpti | |
} | ||
} | ||
|
||
return runWithTimer('cmd_all_timing_network_ms')(async () => { | ||
const response = await retryAwareRequest( | ||
{request: performRequest, url}, | ||
responseOptions?.handleErrors === false ? undefined : errorHandler(api), | ||
unauthorizedHandler, | ||
) | ||
const executeWithTimer = () => | ||
runWithTimer('cmd_all_timing_network_ms')(async () => { | ||
const response = await retryAwareRequest( | ||
{request: performRequest, url}, | ||
responseOptions?.handleErrors === false ? undefined : errorHandler(api), | ||
unauthorizedHandler, | ||
) | ||
|
||
if (responseOptions?.onResponse) { | ||
responseOptions.onResponse(response) | ||
} | ||
if (responseOptions?.onResponse) { | ||
responseOptions.onResponse(response) | ||
} | ||
|
||
return response.data | ||
}) | ||
return response.data | ||
}) | ||
|
||
const {cacheTTL, cacheExtraKey} = options | ||
|
||
// If there is no cache config for this query, just execute it and return the result. | ||
if (cacheTTL === undefined) { | ||
return executeWithTimer() | ||
} | ||
|
||
// The cache key is a combination of the hashed query and variables, with an optional extra key provided by the user. | ||
const queryHash = nonRandomUUID(queryAsString) | ||
const variablesHash = nonRandomUUID(JSON.stringify(variables ?? {})) | ||
const cacheKey: GraphQLRequestKey = `q-${queryHash}-${variablesHash}-${cacheExtraKey ?? ''}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the CLI version? what do you think? |
||
|
||
const result = await cacheRetrieveOrRepopulate( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is an existing function. But where are these being stored? Should they go into a sub-folder in |
||
cacheKey, | ||
async () => { | ||
const result = await executeWithTimer() | ||
return JSON.stringify(result) | ||
}, | ||
cacheTTLToMs(cacheTTL), | ||
) | ||
|
||
return JSON.parse(result) as TResult | ||
} | ||
|
||
async function logLastRequestIdFromResponse(response: GraphQLResponse<unknown>) { | ||
|
@@ -143,3 +177,26 @@ export async function graphqlRequestDoc<TResult, TVariables extends Variables>( | |
queryAsString: resolveRequestDocument(options.query).query, | ||
}) | ||
} | ||
|
||
function cacheTTLToMs(cacheTTL: CacheTTL) { | ||
const oneHour = 1000 * 60 * 60 | ||
const oneDay = 1000 * 60 * 60 * 24 | ||
switch (cacheTTL) { | ||
case '1h': | ||
return oneHour | ||
case '6h': | ||
return oneHour * 6 | ||
case '12h': | ||
return oneHour * 12 | ||
case '1d': | ||
return oneDay | ||
case '3d': | ||
return oneDay * 3 | ||
case '7d': | ||
return oneDay * 7 | ||
case '14d': | ||
return oneDay * 14 | ||
case '30d': | ||
return oneDay * 30 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible to test without the mock? Maybe if we could set the file system location?