Merge pull request #18042 from mozilla/random-spread-strapi-memcache-bust

feat(auth): fixes for Strapi
This commit is contained in:
Julian Poyourow 2024-11-19 13:55:12 -08:00 коммит произвёл GitHub
Родитель b42e55ddb9 a76ced3e04
Коммит 0b7c299e0f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 33 добавлений и 13 удалений

Просмотреть файл

@ -50,7 +50,7 @@ export class StrapiClient {
event: 'response', event: 'response',
listener: (response: EventResponse) => void listener: (response: EventResponse) => void
) => EventEmitter; ) => EventEmitter;
private graphqlMemCache: Record<string, unknown> = {}; private graphqlMemCache: Record<string, Promise<unknown> | undefined> = {};
constructor( constructor(
private strapiClientConfig: StrapiClientConfig, private strapiClientConfig: StrapiClientConfig,
@ -103,21 +103,35 @@ export class StrapiClient {
}; };
if (this.graphqlMemCache[cacheKey]) { if (this.graphqlMemCache[cacheKey]) {
this.emitter.emit('response', { let response: Result | undefined;
...emitterResponse, try {
requestEndTime: emitterResponse.requestStartTime, response = (await this.graphqlMemCache[cacheKey]) as Result;
elapsed: 0, } catch (e) {
cache: true, // Do nothing, fall through to do the actual query since the cached instance failed
}); }
return this.graphqlMemCache[cacheKey] as Result;
if (response) {
this.emitter.emit('response', {
...emitterResponse,
requestEndTime: emitterResponse.requestStartTime,
elapsed: 0,
cache: true,
});
return response;
}
} }
try { try {
const response = await this.client.request<Result, any>({ const response = this.client.request<Result, any>({
document: query, document: query,
variables, variables,
}); });
this.graphqlMemCache[cacheKey] = response;
await response;
const requestEndTime = Date.now(); const requestEndTime = Date.now();
this.emitter.emit('response', { this.emitter.emit('response', {
...emitterResponse, ...emitterResponse,
@ -125,8 +139,6 @@ export class StrapiClient {
requestEndTime, requestEndTime,
}); });
this.graphqlMemCache[cacheKey] = response;
return response; return response;
} catch (e) { } catch (e) {
const requestEndTime = Date.now(); const requestEndTime = Date.now();
@ -151,8 +163,10 @@ export class StrapiClient {
const cacheTTL = const cacheTTL =
(this.strapiClientConfig.memCacheTTL || DEFAULT_MEM_CACHE_TTL) * 1000; (this.strapiClientConfig.memCacheTTL || DEFAULT_MEM_CACHE_TTL) * 1000;
const randomSpread = Math.floor(Math.random() * 100);
setInterval(() => { setInterval(() => {
this.graphqlMemCache = {}; this.graphqlMemCache = {};
}, cacheTTL); }, cacheTTL + randomSpread);
} }
} }

Просмотреть файл

@ -138,7 +138,13 @@ async function run(config) {
); );
Container.set(PromotionCodeManager, promotionCodeManager); Container.set(PromotionCodeManager, promotionCodeManager);
if (config.cms.enabled) { if (
config.cms.enabled ||
(config.cms.strapiClient &&
config.cms.strapiClient.graphqlApiUri &&
config.cms.strapiClient.apiKey &&
config.cms.strapiClient.firestoreCacheCollectionName)
) {
const strapiClientConfig = config.cms.strapiClient; const strapiClientConfig = config.cms.strapiClient;
const { graphqlApiUri, apiKey, firestoreCacheCollectionName } = const { graphqlApiUri, apiKey, firestoreCacheCollectionName } =
strapiClientConfig; strapiClientConfig;