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' });

View file

@ -18,6 +18,10 @@ interface Config {
SEARXNG: string;
OLLAMA: string;
};
REDIS: {
HOST: string;
PORT: number;
};
}
type RecursivePartial<T> = {
@ -44,7 +48,8 @@ export const getSearxngApiEndpoint = () =>
process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG;
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;
export const getRedisHost = () => loadConfig().REDIS.HOST;
export const getRedisPort = () => loadConfig().REDIS.PORT;
export const updateConfig = (config: RecursivePartial<Config>) => {
const currentConfig = loadConfig();

15
src/utils/redisClient.ts Normal file
View file

@ -0,0 +1,15 @@
import { createClient } from 'redis';
import { getRedisHost, getRedisPort } from '../config';
import logger from './logger';
const redisUrl = `redis://${getRedisHost()}:${getRedisPort()}`;
const client = createClient({ url: redisUrl });
client.on('error', (err) => {
logger.error(`Redis Client Error: ${err}`);
});
client
.connect()
.then(() => logger.info('Connected to Redis'))
.catch((err) => logger.error(`Redis connection error: ${err}`));
export default client;