Skip to content

Commit

Permalink
[bugfix] - Convert data to string before setting cache (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Pedro da Silva authored Sep 21, 2023
1 parent 195d8ec commit 558ddb8
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 301 deletions.
10 changes: 7 additions & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"prettier:dryrun": "prettier -l \"./**/*.ts\"",
"eslint:dryrun": "eslint \"./**/*.ts\" --max-warnings=0",
"prettier": "prettier --write \"./**/*.ts\"",
"eslint": "eslint --fix \"./**/*.ts\" --max-warnings=0"
"eslint": "eslint --fix \"./**/*.ts\" --max-warnings=0",
"postinstall": "ts-patch install"
},
"devDependencies": {
"@babel/cli": "7.22.5",
Expand All @@ -41,7 +42,9 @@
"express-status-monitor": "1.3.4",
"mocha": "10.2.0",
"nodemon": "3.0.1",
"prettier": "3.0.0"
"prettier": "3.0.0",
"ts-node": "10.9.1",
"ts-patch": "3.0.2"
},
"dependencies": {
"@appsignal/nodejs": "3.0.14",
Expand Down Expand Up @@ -69,6 +72,7 @@
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
"twilio": "4.16.0",
"typescript": "5.2.2"
"typescript": "5.2.2",
"typia": "5.0.4"
}
}
5 changes: 3 additions & 2 deletions packages/api/src/middlewares/cache-redis.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { json as JSON } from 'typia';
import { NextFunction } from 'express';
import { RequestWithUser } from './core';
import { database } from '@impactmarket/core';
Expand All @@ -18,13 +19,13 @@ export const cache =
'__express__' + (req.originalUrl || req.url) + (useUserCache && req.user ? `user${req.user!.userId}` : '');
const cachedBody = await redis.get(key);
if (cachedBody) {
res.send(cachedBody);
res.send(JSON.isParse(cachedBody));
} else {
res.sendResponse = res.send;
res.send = (body: any) => {
// Only cache if the response is 200
if (res.statusCode === 200) {
redis.set(key, body, 'EX', duration);
redis.set(key, JSON.stringify(body), 'EX', duration);
}
res.sendResponse(body);
};
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/routes/v2/claimLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Router } from 'express';

import { ClaimLocationController } from '../../controllers/v2/claimLocation';
import { authenticateToken } from '../../middlewares';
import { cache } from '../../middlewares/cache-redis';
import { cacheIntervals } from '../../utils/api';
import claimLocationValidators from '../../validators/claimLocation';

export default (app: Router): void => {
Expand All @@ -23,8 +25,7 @@ export default (app: Router): void => {
* "403":
* description: "Invalid input"
*/
route.get('/', controller.getAll);
// route.get('/', cache(cacheIntervals.oneDay), controller.getAll);
route.get('/', cache(cacheIntervals.oneDay), controller.getAll);

/**
* @swagger
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/routes/v2/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export default (app: Router): void => {

route.get('/numbers', cache(cacheIntervals.oneHour), globalController.numbers);

// temporarly remove cache
route.get('/demographics', globalController.globalDemographics);
route.get('/demographics', cache(cacheIntervals.oneHour), globalController.globalDemographics);
};
10 changes: 8 additions & 2 deletions packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
"~services/*": ["services/*"],
"~utils/*": ["utils/*"],
"~config/*": ["config/*"]
}
},
},
"plugins": [
{
"transform": "typia/lib/transform"
}
],
"strictNullChecks": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 558ddb8

Please sign in to comment.