diff --git a/packages/backend-modules/voting/lib/Questionnaire.js b/packages/backend-modules/voting/lib/Questionnaire.js index 7c36983d58..9d4bc0c37c 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('./cache') const transformQuestion = (q, questionnaire) => ({ ...q.typePayload, @@ -35,18 +36,23 @@ 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 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, +}