Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] - Convert data to string before setting cache #906

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 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,10 @@
"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",
"typescript": "5.2.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think you don't need typescript here since you have it on dependencies

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right!

},
"dependencies": {
"@appsignal/nodejs": "3.0.14",
Expand Down Expand Up @@ -69,6 +73,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
Loading