Files
directus/api/src/middleware/cache.ts
2020-09-08 18:28:23 -04:00

26 lines
639 B
TypeScript

import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import env from '../env';
import { getCacheKey } from '../utils/get-cache-key';
import cache from '../cache';
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
if (req.method.toLowerCase() !== 'get') return next();
if (env.CACHE_ENABLED !== true) return next();
if (!cache) return next();
const key = getCacheKey(req);
const cachedData = await cache.get(key);
console.log(key);
if (cachedData) {
return res.json(cachedData);
} else {
return next();
}
});
export default checkCacheMiddleware;