From a9ad8f94509fc211a0f6cf0389b0501e21e33277 Mon Sep 17 00:00:00 2001 From: Mohammad Ranjbar Z Date: Thu, 5 Oct 2023 12:34:50 +0330 Subject: [PATCH 1/2] Put campaign projects cache in redis instead of saving that in memory --- src/services/campaignService.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/services/campaignService.ts b/src/services/campaignService.ts index d3c31707d..129e0a0cc 100644 --- a/src/services/campaignService.ts +++ b/src/services/campaignService.ts @@ -10,6 +10,7 @@ import { ProjectResolverWorker } from '../workers/projectsResolverWorker'; import { QueryBuilder } from 'typeorm/query-builder/QueryBuilder'; import { findAllActiveCampaigns } from '../repositories/campaignRepository'; import { logger } from '../utils/logger'; +import { getRedisObject, setObjectInRedis } from '../redis'; const projectFiltersCacheDuration = Number(process.env.PROJECT_FILTERS_THREADS_POOL_DURATION) || 60000; @@ -41,13 +42,17 @@ const createFetchCampaignProjectsQuery = ( return projectsQueryParams; }; -let projectCampaignCache: { [key: number]: string[] } = {}; +const PROJECT_CAMPAIGN_CACHE_REDIS_KEY = + 'projectCampaignCache-for-projectBySlug'; -export const getAllProjectsRelatedToActiveCampaigns = (): { +export const getAllProjectsRelatedToActiveCampaigns = async (): Promise<{ [key: number]: string[]; -} => { +}> => { + const projectCampaignCache = await getRedisObject( + PROJECT_CAMPAIGN_CACHE_REDIS_KEY, + ); // It returns all project and campaigns( excluding manuallySelectedCampaign) - return projectCampaignCache; + return projectCampaignCache || {}; }; export const cacheProjectCampaigns = async (): Promise => { @@ -67,10 +72,15 @@ export const cacheProjectCampaigns = async (): Promise => { : (newProjectCampaignCache[project.id] = [campaign.slug]); } } - projectCampaignCache = newProjectCampaignCache; + await setObjectInRedis({ + key: PROJECT_CAMPAIGN_CACHE_REDIS_KEY, + value: newProjectCampaignCache, + // cronjob would fill it every 10 minutes so the expiration doesnt matter + expiration: 60 * 60 * 24 * 1, // 1 day + }); logger.debug( 'cacheProjectCampaigns() ended successfully, projectCampaignCache size ', - Object.keys(projectCampaignCache).length, + Object.keys(newProjectCampaignCache).length, ); }; From cafffc9497376f77b9a40931d54d2a353c75ec2e Mon Sep 17 00:00:00 2001 From: Mohammad Ranjbar Z Date: Thu, 5 Oct 2023 13:39:15 +0330 Subject: [PATCH 2/2] Change default value of CACHE_PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION --- config/example.env | 4 ++-- config/test.env | 4 ++-- src/services/cronJobs/updateProjectCampaignsCacheJob.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/example.env b/config/example.env index 9e61f4c03..2f03bd6f9 100644 --- a/config/example.env +++ b/config/example.env @@ -206,5 +206,5 @@ QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION=60000 # OPTIONAL - default: Every 10 minutes PROJECT_CAMPAIGNS_CACHE_DURATION=600000 -# OPTIONAL - default: */10 * * * * * ( Every 10 minutes) -PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=*/10 * * * * * +# OPTIONAL - default: *0 */5 * * * ( Every 5 minutes) +PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=0 */5 * * * diff --git a/config/test.env b/config/test.env index 71fce3f69..5f1398a3b 100644 --- a/config/test.env +++ b/config/test.env @@ -179,5 +179,5 @@ QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION=1 PROJECT_CAMPAIGNS_CACHE_DURATION=1 -# OPTIONAL - default: */10 * * * * * ( Every 10 minutes) -PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=*/10 * * * * * +# OPTIONAL - default: *0 */5 * * * ( Every 5 minutes) +PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=0 */5 * * * diff --git a/src/services/cronJobs/updateProjectCampaignsCacheJob.ts b/src/services/cronJobs/updateProjectCampaignsCacheJob.ts index 3797278ab..b29b11cd6 100644 --- a/src/services/cronJobs/updateProjectCampaignsCacheJob.ts +++ b/src/services/cronJobs/updateProjectCampaignsCacheJob.ts @@ -8,7 +8,7 @@ import { CacheProjectCampaignsWorker } from '../../workers/cacheProjectCampaigns // every 10 minutes const cronJobTime = (config.get('CACHE_PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION') as string) || - '*/10 * * * *'; + '0 */5 * * *'; const projectsFiltersThreadPool: Pool< ModuleThread