UPDATE new features = redis in docker and Caching Mechanism

This commit is contained in:
redesyef 2024-09-19 15:02:30 -05:00
parent 1fcd64ad42
commit 27a084b9f0
9 changed files with 23961 additions and 4654 deletions

View file

@ -1,11 +1,12 @@
import { startWebSocketServer } from './websocket';
import express from 'express';
import { Request, Response, NextFunction } from 'express';
import cors from 'cors';
import http from 'http';
import routes from './routes';
import { getPort } from './config';
import logger from './utils/logger';
import redisClient from './utils/redisClient';
const port = getPort();
const app = express();
@ -18,6 +19,43 @@ const corsOptions = {
app.use(cors(corsOptions));
app.use(express.json());
app.use(async (req: Request, res: Response, next: NextFunction) => {
const cache = req.query.cache as string;
if (cache === '1') {
const cacheKey = req.originalUrl || req.url;
try {
const cachedData = await redisClient.get(cacheKey);
if (cachedData) {
logger.info(`Cache hit for ${cacheKey}`);
const jsonData = JSON.parse(cachedData);
return res.json(JSON.parse(jsonData));
} else {
const originalSend = res.send.bind(res);
res.send = (body: any) => {
const result = originalSend(body);
redisClient
.setEx(cacheKey, 3600, JSON.stringify(body))
.then(() => logger.info(`Cache set for ${cacheKey}`))
.catch((err) => logger.error(`Redis setEx error: ${err}`));
return result;
};
next();
}
} catch (error) {
logger.error(`Unexpected error: ${error}`);
next();
}
} else {
next();
}
});
app.use('/api', routes);
app.get('/api', (_, res) => {
res.status(200).json({ status: 'ok' });