Skip to content

Commit

Permalink
Added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
taha-abbasi committed Mar 31, 2023
1 parent 1da63e8 commit 6749a33
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 13 deletions.
25 changes: 25 additions & 0 deletions build/cacheMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_cache_1 = __importDefault(require("node-cache"));
const cache = new node_cache_1.default({ stdTTL: 60 }); // 60 seconds TTL
const cacheMiddleware = (duration) => {
return (req, res, next) => {
const key = req.originalUrl;
const cachedResponse = cache.get(key);
if (cachedResponse) {
res.send(cachedResponse);
}
else {
const originalSend = res.send.bind(res);
res.send = ((body) => {
cache.set(key, body, duration);
originalSend(body);
});
next();
}
};
};
exports.default = cacheMiddleware;
30 changes: 24 additions & 6 deletions build/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ const body_parser_1 = __importDefault(require("body-parser"));
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const getSupplyAcrossNetworks_1 = require("./getSupplyAcrossNetworks");
const config_1 = require("./config");
const cacheMiddleware_1 = __importDefault(require("./cacheMiddleware"));
const app = (0, express_1.default)();
const port = process.env.PORT || 8080;
// const cache = new NodeCache({ stdTTL: 60 }); // 60 seconds TTL
app.use(body_parser_1.default.json());
app.get("/totalSupplyAcrossNetworks", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
// const cacheMiddleware = (duration: number) => {
// return (req: Request, res: Response, next: NextFunction) => {
// const key = req.originalUrl;
// const cachedResponse = cache.get(key);
// if (cachedResponse) {
// res.send(cachedResponse);
// } else {
// const originalSend = res.send.bind(res);
// res.send = ((body: any) => {
// cache.set(key, body, duration);
// originalSend(body);
// }) as Response['send'];
// next();
// }
// };
// }
app.get("/totalSupplyAcrossNetworks", (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand All @@ -36,7 +54,7 @@ app.get("/totalSupplyAcrossNetworks", (req, res) => __awaiter(void 0, void 0, vo
res.status(500).json({ error: 'An error occurred while fetching the total supply.' });
}
}));
app.get('/totalSupply', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.get('/totalSupply', (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand All @@ -52,7 +70,7 @@ app.get('/totalSupply', (req, res) => __awaiter(void 0, void 0, void 0, function
res.status(500).send('Error getting total supply');
}
}));
app.get("/nonCirculatingSupplyAddresses", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.get("/nonCirculatingSupplyAddresses", (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
res.status(400).json({ error: 'Both tokenContractAddress and chainId must be provided as query parameters.' });
Expand All @@ -61,7 +79,7 @@ app.get("/nonCirculatingSupplyAddresses", (req, res) => __awaiter(void 0, void 0
const nonCirculatingSupplyAddressConfigurations = yield (0, config_1.getNonCirculatingSupplyAddressConfigurations)(tokenContractAddress, Number(chainId));
res.json(nonCirculatingSupplyAddressConfigurations);
}));
app.get('/nonCirculatingSupplyBalancesByAddress', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.get('/nonCirculatingSupplyBalancesByAddress', (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand All @@ -76,7 +94,7 @@ app.get('/nonCirculatingSupplyBalancesByAddress', (req, res) => __awaiter(void 0
res.status(500).json({ error: 'Failed to fetch non-circulating supply balances' });
}
}));
app.get('/nonCirculatingSupplyBalance', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.get('/nonCirculatingSupplyBalance', (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand All @@ -92,7 +110,7 @@ app.get('/nonCirculatingSupplyBalance', (req, res) => __awaiter(void 0, void 0,
res.status(500).json({ error: 'Failed to fetch non-circulating supply balances' });
}
}));
app.get('/circulatingSupplyBalance', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.get('/circulatingSupplyBalance', (0, cacheMiddleware_1.default)(60), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { tokenContractAddress, chainId } = req.query;
if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand Down
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bignumber.js": "^9.1.1",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"node-cache": "^5.1.2",
"web3": "^1.9.0"
},
"devDependencies": {
Expand Down
25 changes: 25 additions & 0 deletions src/cacheMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response, NextFunction } from "express";
import NodeCache from "node-cache";

const cache = new NodeCache({ stdTTL: 60 }); // 60 seconds TTL

const cacheMiddleware = (duration: number) => {
return (req: Request, res: Response, next: NextFunction) => {
const key = req.originalUrl;
const cachedResponse = cache.get(key);

if (cachedResponse) {
res.send(cachedResponse);
} else {
const originalSend = res.send.bind(res);

res.send = ((body: any) => {
cache.set(key, body, duration);
originalSend(body);
}) as Response['send'];

next();
}
};
}
export default cacheMiddleware;
36 changes: 29 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
// src/server.ts
import express from "express";
import express, { Request, Response, NextFunction } from "express";
import bodyParser from 'body-parser';
import BigNumber from "bignumber.js";
import { getTotalSupplyAcrossNetworks, getNonCirculatingSupplyBalances } from "./getSupplyAcrossNetworks";
import { getNetworkConfigurations, getNonCirculatingSupplyAddressConfigurations } from "./config";
import { NonCirculatingSupplyBalance } from './types';
import cacheMiddleware from './cacheMiddleware';



const app = express();
const port = process.env.PORT || 8080;
// const cache = new NodeCache({ stdTTL: 60 }); // 60 seconds TTL

app.use(bodyParser.json());

app.get("/totalSupplyAcrossNetworks", async (req, res) => {
// const cacheMiddleware = (duration: number) => {
// return (req: Request, res: Response, next: NextFunction) => {
// const key = req.originalUrl;
// const cachedResponse = cache.get(key);

// if (cachedResponse) {
// res.send(cachedResponse);
// } else {
// const originalSend = res.send.bind(res);

// res.send = ((body: any) => {
// cache.set(key, body, duration);
// originalSend(body);
// }) as Response['send'];

// next();
// }
// };
// }

app.get("/totalSupplyAcrossNetworks", cacheMiddleware(60), async (req, res) => {
try {
const { tokenContractAddress, chainId } = req.query;

Expand All @@ -30,7 +52,7 @@ app.get("/totalSupplyAcrossNetworks", async (req, res) => {
}
});

app.get('/totalSupply', async (req, res) => {
app.get('/totalSupply', cacheMiddleware(60), async (req, res) => {
try {
const { tokenContractAddress, chainId } = req.query;

Expand All @@ -48,7 +70,7 @@ app.get('/totalSupply', async (req, res) => {
}
});

app.get("/nonCirculatingSupplyAddresses", async (req, res) => {
app.get("/nonCirculatingSupplyAddresses", cacheMiddleware(60), async (req, res) => {
const { tokenContractAddress, chainId } = req.query;

if (typeof tokenContractAddress !== 'string' || typeof chainId !== 'string') {
Expand All @@ -59,7 +81,7 @@ app.get("/nonCirculatingSupplyAddresses", async (req, res) => {
res.json(nonCirculatingSupplyAddressConfigurations);
});

app.get('/nonCirculatingSupplyBalancesByAddress', async (req, res) => {
app.get('/nonCirculatingSupplyBalancesByAddress', cacheMiddleware(60), async (req, res) => {
try {
const { tokenContractAddress, chainId } = req.query;

Expand All @@ -75,7 +97,7 @@ app.get('/nonCirculatingSupplyBalancesByAddress', async (req, res) => {
}
});

app.get('/nonCirculatingSupplyBalance', async (req, res) => {
app.get('/nonCirculatingSupplyBalance', cacheMiddleware(60), async (req, res) => {
try {
const { tokenContractAddress, chainId } = req.query;

Expand All @@ -92,7 +114,7 @@ app.get('/nonCirculatingSupplyBalance', async (req, res) => {
}
});

app.get('/circulatingSupplyBalance', async (req, res) => {
app.get('/circulatingSupplyBalance', cacheMiddleware(60), async (req, res) => {
try {
const { tokenContractAddress, chainId } = req.query;

Expand Down

0 comments on commit 6749a33

Please sign in to comment.