Skip to content

Commit 00a624d

Browse files
author
FalkWolsky
committed
Enable Caching for the Node-Service
1 parent 6952535 commit 00a624d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

server/node-service/src/routes/apiRouter.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,43 @@ import * as npmControllers from "../controllers/npm";
55

66
const apiRouter = express.Router();
77

8+
// In-memory cache object
9+
const cache: { [key: string]: any } = {};
10+
11+
// Middleware to cache responses for specific routes
12+
function cacheMiddleware(req: express.Request, res: express.Response, next: express.NextFunction) {
13+
const cacheKey = req.originalUrl;
14+
15+
// Check if the response is already cached
16+
if (cache[cacheKey]) {
17+
return res.json(cache[cacheKey]); // Return cached JSON object if it exists
18+
}
19+
20+
// Override res.json instead of res.send to store response in cache as JSON
21+
const originalJson = res.json.bind(res);
22+
res.json = (body: any) => {
23+
cache[cacheKey] = body; // Cache the JSON response
24+
originalJson(body); // Send the JSON response
25+
return res;
26+
};
27+
28+
next();
29+
}
30+
831
apiRouter.post("/runJs", jsControllers.runJavascript);
932
apiRouter.post("/batchRunJs", jsControllers.batchRunJavascript);
1033

11-
apiRouter.get("/plugins", pluginControllers.listPlugins);
34+
apiRouter.get("/plugins", cacheMiddleware, pluginControllers.listPlugins);
1235
apiRouter.post("/runPluginQuery", pluginControllers.runPluginQuery);
13-
apiRouter.post("/getPluginDynamicConfig", pluginControllers.getDynamicDef);
36+
apiRouter.post("/getPluginDynamicConfig", cacheMiddleware, pluginControllers.getDynamicDef);
1437
apiRouter.post("/validatePluginDataSourceConfig", pluginControllers.validatePluginDataSourceConfig);
1538

1639
// routes for npm registry and package fetching with config called by the api-service
1740
apiRouter.post("/npm/registry/*", npmControllers.fetchRegistryWithConfig);
1841
apiRouter.post("/npm/package/*", npmControllers.fetchPackageFileWithConfig);
1942

2043
// temporary routes for testing npm registry and package routes by directly calling the node-service
21-
apiRouter.get("/npm/registry/*", npmControllers.fetchRegistry);
22-
apiRouter.get("/npm/package/*", npmControllers.fetchPackageFile);
44+
// apiRouter.get("/npm/registry/*", npmControllers.fetchRegistry);
45+
// apiRouter.get("/npm/package/*", npmControllers.fetchPackageFile);
2346

2447
export default apiRouter;

0 commit comments

Comments
 (0)