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

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

@ -138,7 +138,13 @@ async function run(config) {
);
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 { graphqlApiUri, apiKey, firestoreCacheCollectionName } =
strapiClientConfig;