mirror of
https://github.com/directus/directus.git
synced 2026-02-08 15:05:03 -05:00
change so can send cache live variables
This commit is contained in:
@@ -16,39 +16,41 @@ const redisClient = redis.createClient({
|
||||
password: env.REDIS_PASSWORD,
|
||||
});
|
||||
|
||||
const cacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
// make the key of the cache the URL
|
||||
// need to check that this will work for all endpoints
|
||||
// node cache service
|
||||
const key = req.url;
|
||||
const cacheMiddleware = (TTL: number, checkDeath: number) =>
|
||||
asyncHandler(async (req, res, next) => {
|
||||
// make the key of the cache the URL
|
||||
// need to check that this will work for all endpoints
|
||||
// node cache service
|
||||
const key = req.url;
|
||||
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
if (!redisClient) {
|
||||
throw new RedisNotFoundException('Redis client does not exist');
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
if (!redisClient) {
|
||||
throw new RedisNotFoundException('Redis client does not exist');
|
||||
}
|
||||
|
||||
redisClient.get(key, (error, resultData) => {
|
||||
if (error) {
|
||||
throw new RedisNotFoundException('Error retreiving redis cache');
|
||||
}
|
||||
|
||||
if (resultData) {
|
||||
res.send(resultData);
|
||||
}
|
||||
if (!resultData) {
|
||||
// set data and then return
|
||||
redisClient.setex(key, TTL, JSON.stringify(res.json));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// use the node cache
|
||||
// set for ten minutes
|
||||
const nodeCache = new CacheService(TTL, checkDeath);
|
||||
|
||||
nodeCache.getCache(key, JSON.stringify(res.json));
|
||||
}
|
||||
|
||||
redisClient.get(key, (error, resultData) => {
|
||||
if (error) {
|
||||
throw new RedisNotFoundException('Error retreiving redis cache');
|
||||
}
|
||||
|
||||
if (resultData) {
|
||||
res.send(resultData);
|
||||
}
|
||||
if (!resultData) {
|
||||
// set data and then return
|
||||
redisClient.setex(key, 600, JSON.stringify(res.json));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// use the node cache
|
||||
const nodeCache = new CacheService(600, 600);
|
||||
|
||||
nodeCache.getCache(key, JSON.stringify(res.json));
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
return next();
|
||||
});
|
||||
|
||||
export default cacheMiddleware;
|
||||
|
||||
Reference in New Issue
Block a user