Skip to content

Commit c91a59b

Browse files
authored
Merge pull request #2269 from Plant-for-the-Planet-org/hotfix/refactor-cache-prefix-logic
Refactor cache key prefix logic
2 parents 3f16d2b + cbe84ec commit c91a59b

File tree

8 files changed

+18
-27
lines changed

8 files changed

+18
-27
lines changed

pages/api/data-explorer/map/distinct-species/[projectId].ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
UncleanDistinctSpecies,
1111
} from '../../../../../src/features/common/types/dataExplorer';
1212
import redisClient from '../../../../../src/redis-client';
13+
import { cacheKeyPrefix } from '../../../../../src/utils/constants/cacheKeyPrefix';
1314

1415
const ONE_HOUR_IN_SEC = 60 * 60;
1516
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
1617

17-
const KEY = 'DISTINCT_SPECIES';
18+
const KEY = `${cacheKeyPrefix}_DISTINCT_SPECIES`;
1819

1920
const handler = nc<NextApiRequest, NextApiResponse>();
2021

pages/api/data-explorer/map/sites/[projectId].ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
UncleanSite,
1111
} from '../../../../../src/features/common/types/dataExplorer';
1212
import redisClient from '../../../../../src/redis-client';
13+
import { cacheKeyPrefix } from '../../../../../src/utils/constants/cacheKeyPrefix';
1314

1415
const ONE_HOUR_IN_SEC = 60 * 60;
1516
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
1617

17-
const KEY = 'SITES';
18+
const KEY = `${cacheKeyPrefix}_SITES`;
1819

1920
const handler = nc<NextApiRequest, NextApiResponse>();
2021

pages/api/data-explorer/species-planted.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { getCachedKey } from '../../../src/utils/getCachedKey';
99
import { ISpeciesPlanted } from '../../../src/features/common/types/dataExplorer';
1010
import redisClient from '../../../src/redis-client';
11+
import { cacheKeyPrefix } from '../../../src/utils/constants/cacheKeyPrefix';
1112

1213
const ONE_HOUR_IN_SEC = 60 * 60;
1314
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
@@ -26,11 +27,7 @@ handler.post(async (req, response) => {
2627

2728
const { projectId, startDate, endDate } = req.body;
2829

29-
const cachingKeyPrefix =
30-
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
31-
'env_missing';
32-
33-
const CACHE_KEY = `${cachingKeyPrefix}_SPECIES_PLANTED__${getCachedKey(
30+
const CACHE_KEY = `${cacheKeyPrefix}_SPECIES_PLANTED__${getCachedKey(
3431
projectId,
3532
startDate,
3633
endDate

pages/api/data-explorer/total-species-planted.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { getCachedKey } from '../../../src/utils/getCachedKey';
99
import { TotalSpeciesPlanted } from '../../../src/features/common/types/dataExplorer';
1010
import redisClient from '../../../src/redis-client';
11+
import { cacheKeyPrefix } from '../../../src/utils/constants/cacheKeyPrefix';
1112

1213
const ONE_HOUR_IN_SEC = 60 * 60;
1314
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
@@ -26,11 +27,7 @@ handler.post(async (req, response) => {
2627

2728
const { projectId, startDate, endDate } = req.body;
2829

29-
const cachingKeyPrefix =
30-
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
31-
'env_missing';
32-
33-
const CACHE_KEY = `${cachingKeyPrefix}_TOTAL_SPECIES_PLANTED__${getCachedKey(
30+
const CACHE_KEY = `${cacheKeyPrefix}_TOTAL_SPECIES_PLANTED__${getCachedKey(
3431
projectId,
3532
startDate,
3633
endDate

pages/api/data-explorer/total-trees-planted.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { getCachedKey } from '../../../src/utils/getCachedKey';
99
import { TotalTreesPlanted } from '../../../src/features/common/types/dataExplorer';
1010
import redisClient from '../../../src/redis-client';
11+
import { cacheKeyPrefix } from '../../../src/utils/constants/cacheKeyPrefix';
1112

1213
const ONE_HOUR_IN_SEC = 60 * 60;
1314
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
@@ -26,11 +27,7 @@ handler.post(async (req, response) => {
2627

2728
const { projectId, startDate, endDate } = req.body;
2829

29-
const cachingKeyPrefix =
30-
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
31-
'env_missing';
32-
33-
const CACHE_KEY = `${cachingKeyPrefix}_TOTAL_TREES_PLANTED__${getCachedKey(
30+
const CACHE_KEY = `${cacheKeyPrefix}_TOTAL_TREES_PLANTED__${getCachedKey(
3431
projectId,
3532
startDate,
3633
endDate

pages/api/data-explorer/trees-planted.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
IYearlyFrame,
1515
} from '../../../src/features/common/types/dataExplorer';
1616
import redisClient from '../../../src/redis-client';
17+
import { cacheKeyPrefix } from '../../../src/utils/constants/cacheKeyPrefix';
1718

1819
const ONE_HOUR_IN_SEC = 60 * 60;
1920
const TWO_HOURS = ONE_HOUR_IN_SEC * 2;
@@ -33,11 +34,7 @@ handler.post(async (req, response) => {
3334
const { projectId, startDate, endDate } = req.body;
3435
const { timeFrame } = req.query;
3536

36-
const cachingKeyPrefix =
37-
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
38-
'env_missing';
39-
40-
const CACHE_KEY = `${cachingKeyPrefix}_TREES_PLANTED__${getCachedKey(
37+
const CACHE_KEY = `${cacheKeyPrefix}_TREES_PLANTED__${getCachedKey(
4138
projectId,
4239
startDate,
4340
endDate,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// If the API_ENDPOINT is https://app-staging.planet.com, the cachePrefix will be 'staging'
2+
export const cacheKeyPrefix =
3+
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
4+
'env_missing';

src/utils/multiTenancy/helpers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Tenant } from '@planet-sdk/common/build/types/tenant';
22
import redisClient from '../../redis-client';
3+
import { cacheKeyPrefix } from '../constants/cacheKeyPrefix';
34

45
const ONE_HOUR_IN_SEC = 60 * 60;
56
const FIVE_HOURS = ONE_HOUR_IN_SEC * 5;
@@ -58,11 +59,7 @@ export async function constructPathsForTenantSlug() {
5859
*/
5960
export const getTenantConfig = async (slug: string): Promise<Tenant> => {
6061
try {
61-
// If the API_ENDPOINT is https://app-staging.planet.com, the cachingKeyPrefix will be 'staging'
62-
const cachingKeyPrefix =
63-
process.env.API_ENDPOINT?.replace('https://', '').split('.')[0] ||
64-
'env_missing';
65-
const caching_key = `${cachingKeyPrefix}_TENANT_CONFIG_${slug}`;
62+
const caching_key = `${cacheKeyPrefix}_TENANT_CONFIG_${slug}`;
6663

6764
const tenant =
6865
redisClient !== null ? await redisClient.get<Tenant>(caching_key) : null;

0 commit comments

Comments
 (0)