From d621b4c1a79a0c90367b5013db0168b143170b91 Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 17 Aug 2020 01:33:34 -0700 Subject: [PATCH] fix caching for petTypeLoader Oops, of course, we weren't actually taking proper advantage of the dataloader here! The queries got over-complicated, but more importantly, subsequent requests to the same loader would re-submit the query! I noticed it in the SearchPanel operation, in this Honeycomb trace: https://ui.honeycomb.io/openneo/datasets/dress-to-impress--2020-/trace/aMuhsTjQFZY --- src/server/loaders.js | 47 +++++++++++++++-------------- src/server/query-tests/Item.test.js | 6 +--- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/server/loaders.js b/src/server/loaders.js index fbf29b8..13939c2 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -196,32 +196,35 @@ const buildPetTypeLoader = (db) => }); const buildPetTypeBySpeciesAndColorLoader = (db, loaders) => - new DataLoader(async (speciesAndColorPairs) => { - const conditions = []; - const values = []; - for (const { speciesId, colorId } of speciesAndColorPairs) { - conditions.push("(species_id = ? AND color_id = ?)"); - values.push(speciesId, colorId); - } + new DataLoader( + async (speciesAndColorPairs) => { + const conditions = []; + const values = []; + for (const { speciesId, colorId } of speciesAndColorPairs) { + conditions.push("(species_id = ? AND color_id = ?)"); + values.push(speciesId, colorId); + } - const [rows, _] = await db.execute( - `SELECT * FROM pet_types WHERE ${conditions.join(" OR ")}`, - values - ); + const [rows, _] = await db.execute( + `SELECT * FROM pet_types WHERE ${conditions.join(" OR ")}`, + values + ); - const entities = rows.map(normalizeRow); - const entitiesBySpeciesAndColorPair = new Map( - entities.map((e) => [`${e.speciesId},${e.colorId}`, e]) - ); + const entities = rows.map(normalizeRow); + const entitiesBySpeciesAndColorPair = new Map( + entities.map((e) => [`${e.speciesId},${e.colorId}`, e]) + ); - for (const petType of entities) { - loaders.petTypeLoader.prime(petType.id, petType); - } + for (const petType of entities) { + loaders.petTypeLoader.prime(petType.id, petType); + } - return speciesAndColorPairs.map(({ speciesId, colorId }) => - entitiesBySpeciesAndColorPair.get(`${speciesId},${colorId}`) - ); - }); + return speciesAndColorPairs.map(({ speciesId, colorId }) => + entitiesBySpeciesAndColorPair.get(`${speciesId},${colorId}`) + ); + }, + { cacheKeyFn: ({ speciesId, colorId }) => `${speciesId},${colorId}` } + ); const buildSwfAssetLoader = (db) => new DataLoader(async (swfAssetIds) => { diff --git a/src/server/query-tests/Item.test.js b/src/server/query-tests/Item.test.js index 191af06..774b0b9 100644 --- a/src/server/query-tests/Item.test.js +++ b/src/server/query-tests/Item.test.js @@ -111,14 +111,10 @@ describe("Item", () => { ], ], Array [ - "SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?) OR (species_id = ? AND color_id = ?) OR (species_id = ? AND color_id = ?)", + "SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)", Array [ "54", "75", - "54", - "75", - "54", - "75", ], ], Array [