From 582415b942c70446fd7a2195722eccea0990b9b0 Mon Sep 17 00:00:00 2001 From: Luciana Kolbeck Date: Wed, 19 Jul 2023 18:00:58 +0200 Subject: [PATCH 1/2] fix: added caching to questions query --- .../voting/lib/Questionnaire.js | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/backend-modules/voting/lib/Questionnaire.js b/packages/backend-modules/voting/lib/Questionnaire.js index 7c36983d58..bf5493bb1a 100644 --- a/packages/backend-modules/voting/lib/Questionnaire.js +++ b/packages/backend-modules/voting/lib/Questionnaire.js @@ -5,6 +5,7 @@ const { resultForArchive } = require('./Question') const { resultForValues: rangeResultForValues } = require('./Question/Range') const finalizeLib = require('./finalize') const { shuffle } = require('d3-array') +const { getCache } = require('../../lib/cache') const transformQuestion = (q, questionnaire) => ({ ...q.typePayload, @@ -35,18 +36,24 @@ const getQuestions = async (questionnaire, args = {}, pgdb) => { ...questionnaire, } - const questions = await pgdb.public.questions - .find( - { - questionnaireId: questionnaire.id, - ...(orderFilter ? { order: orderFilter } : {}), - ...(includeHidden ? {} : { hidden: false }), - }, - { orderBy: { order: 'asc' } }, - ) - .then((questions) => - questions.map((q) => transformQuestion(q, questionnaireWithTurnout)), - ) + const questions = getCache( + questionnaire.id, + orderFilter, + includeHidden, + ).cache(async () => { + return pgdb.public.questions + .find( + { + questionnaireId: questionnaire.id, + ...(orderFilter ? { order: orderFilter } : {}), + ...(includeHidden ? {} : { hidden: false }), + }, + { orderBy: { order: 'asc' } }, + ) + .then((questions) => + questions.map((q) => transformQuestion(q, questionnaireWithTurnout)), + ) + }) if (args.shuffle) { // +1 for weights > 0 From be88cdeaa1e4ac23ccdd69bf711e4decf5010e0c Mon Sep 17 00:00:00 2001 From: Luciana Kolbeck Date: Thu, 20 Jul 2023 10:41:54 +0200 Subject: [PATCH 2/2] fix: fixed cache library --- .../voting/lib/Questionnaire.js | 7 +++---- packages/backend-modules/voting/lib/cache.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 packages/backend-modules/voting/lib/cache.js diff --git a/packages/backend-modules/voting/lib/Questionnaire.js b/packages/backend-modules/voting/lib/Questionnaire.js index bf5493bb1a..9d4bc0c37c 100644 --- a/packages/backend-modules/voting/lib/Questionnaire.js +++ b/packages/backend-modules/voting/lib/Questionnaire.js @@ -5,7 +5,7 @@ const { resultForArchive } = require('./Question') const { resultForValues: rangeResultForValues } = require('./Question/Range') const finalizeLib = require('./finalize') const { shuffle } = require('d3-array') -const { getCache } = require('../../lib/cache') +const { getCache } = require('./cache') const transformQuestion = (q, questionnaire) => ({ ...q.typePayload, @@ -36,11 +36,10 @@ const getQuestions = async (questionnaire, args = {}, pgdb) => { ...questionnaire, } - const questions = getCache( - questionnaire.id, + const questions = getCache(questionnaire.id, { orderFilter, includeHidden, - ).cache(async () => { + }).cache(async () => { return pgdb.public.questions .find( { diff --git a/packages/backend-modules/voting/lib/cache.js b/packages/backend-modules/voting/lib/cache.js new file mode 100644 index 0000000000..0ecbcf4fee --- /dev/null +++ b/packages/backend-modules/voting/lib/cache.js @@ -0,0 +1,19 @@ +const createCache = require('@orbiting/backend-modules-republik-crowdfundings/lib/cache') +const { DISABLE_RESOLVER_USER_CACHE } = process.env + +const QUERY_CACHE_TTL_SECONDS = 60 * 60 * 4 // 4 hours + +const getCache = (questionnaireId, args) => + createCache( + { + prefix: `questionnaire:${questionnaireId}`, + key: `questions`, + ttl: QUERY_CACHE_TTL_SECONDS, + disabled: DISABLE_RESOLVER_USER_CACHE, + }, + args, + ) + +module.exports = { + getCache, +}