Skip to content

Commit

Permalink
Put campaign projects cache in redis instead of saving that in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed Oct 5, 2023
1 parent 97d1a2f commit a9ad8f9
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/services/campaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<void> => {
Expand All @@ -67,10 +72,15 @@ export const cacheProjectCampaigns = async (): Promise<void> => {
: (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,
);
};

Expand Down

0 comments on commit a9ad8f9

Please sign in to comment.