mirror of
https://github.com/directus/directus.git
synced 2026-02-04 09:34:56 -05:00
26 lines
639 B
TypeScript
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;
|