45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
|
import { NextFunction, Request, Response } from 'express';
|
||
|
import { HttpException } from '../exceptions/index';
|
||
|
import * as redis from 'redis';
|
||
|
import { Global } from "../global";
|
||
|
import { Duration } from 'moment';
|
||
|
|
||
|
// connect to Redis
|
||
|
const REDIS_URL = process.env.REDIS_URL;
|
||
|
const client = redis.createClient(REDIS_URL);
|
||
|
|
||
|
client.on('connect', () => {
|
||
|
console.log(`connected to redis`);
|
||
|
});
|
||
|
client.on('error', err => {
|
||
|
console.log(`Error: ${err}`);
|
||
|
});
|
||
|
|
||
|
export function redisMiddleware(
|
||
|
duration: any,
|
||
|
) {
|
||
|
console.log(duration);
|
||
|
return (request, response, next) => {
|
||
|
// Global.network = request.headers.network;
|
||
|
let key = process.env.REDIS_KEY + Global.network + request.originalUrl || request.url
|
||
|
client.get(key, function(err, reply){
|
||
|
if(reply && duration!=0){
|
||
|
console.log("key : ", key);
|
||
|
console.log("reply : ", reply);
|
||
|
console.log("---------------------------------------------------------------------------------------");
|
||
|
console.log(`enabled`);
|
||
|
response.send(reply);
|
||
|
}else{
|
||
|
console.log(`raw`);
|
||
|
response.sendResponse = response.send;
|
||
|
response.send = (body) => {
|
||
|
client.set(key, JSON.stringify(body), 'EX', duration, function(err){
|
||
|
//client.set(key, JSON.stringify(body));
|
||
|
response.sendResponse(body);
|
||
|
});
|
||
|
}
|
||
|
next();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|