From 52fbed18cad28db25e597e395d1098c265fefa71 Mon Sep 17 00:00:00 2001 From: Celine Pelletier Date: Fri, 25 Oct 2024 10:40:38 -0400 Subject: [PATCH] feat: SJIP-973 add cache for diffGeneExp --- src/app.ts | 20 +++++++++++++++----- src/middleware/cache.ts | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/app.ts b/src/app.ts index b0f439a..ef7e091 100644 --- a/src/app.ts +++ b/src/app.ts @@ -22,7 +22,12 @@ import { getStatistics, getStudiesStatistics } from './endpoints/statistics'; import { checkSampleIdsAndGene, fetchDiffGeneExp, fetchFacets, fetchSampleGeneExp } from './endpoints/transcriptomics'; import { cacheTTL, esHost, keycloakURL, userApiURL } from './env'; import { globalErrorHandler, globalErrorLogger } from './errors'; -import { STATISTICS_CACHE_ID, STATISTICS_PUBLIC_CACHE_ID, verifyCache } from './middleware/cache'; +import { + STATISTICS_CACHE_ID, + STATISTICS_PUBLIC_CACHE_ID, + TRANSCRIPTOMICS_DIFF_GENE_EXP_CACHE_ID, + verifyCache, +} from './middleware/cache'; import { injectBodyHttpHeaders } from './middleware/injectBodyHttpHeaders'; import { resolveSetIdMiddleware } from './middleware/resolveSetIdInSqon'; @@ -149,11 +154,16 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP res.send({ data }); }); - app.postAsync('/transcriptomics/diffGeneExp', keycloak.protect(), async (req, res) => { - const data = await fetchDiffGeneExp(); + app.postAsync( + '/transcriptomics/diffGeneExp', + [keycloak.protect(), verifyCache(TRANSCRIPTOMICS_DIFF_GENE_EXP_CACHE_ID, cache)], + async (req, res) => { + const data = await fetchDiffGeneExp(); + cache.set(TRANSCRIPTOMICS_DIFF_GENE_EXP_CACHE_ID, data); - res.json(data); - }); + res.json(data); + }, + ); app.postAsync('/transcriptomics/sampleGeneExp', keycloak.protect(), async (req, res) => { const ensembl_gene_id: string = req.body.ensembl_gene_id; diff --git a/src/middleware/cache.ts b/src/middleware/cache.ts index 871dfda..20952e2 100644 --- a/src/middleware/cache.ts +++ b/src/middleware/cache.ts @@ -2,8 +2,8 @@ import { NextFunction, Request, Response } from 'express'; import NodeCache from 'node-cache'; export const STATISTICS_CACHE_ID = 'statistics'; - export const STATISTICS_PUBLIC_CACHE_ID = 'statistics_studies'; +export const TRANSCRIPTOMICS_DIFF_GENE_EXP_CACHE_ID = 'transcriptomics_diff_gene_exp'; export const verifyCache = (cacheId: string, cache: NodeCache) => ( _req: Request,