mirror of
https://github.com/directus/directus.git
synced 2026-01-28 22:37:57 -05:00
changed so can send TTL and death TTL in query parameters
This commit is contained in:
@@ -2,6 +2,7 @@ import express from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import sanitizeQuery from '../middleware/sanitize-query';
|
||||
import useCollection from '../middleware/use-collection';
|
||||
import cacheMiddleware from '../middleware/cache';
|
||||
import ActivityService from '../services/activity';
|
||||
import MetaService from '../services/meta';
|
||||
import { Action } from '../types';
|
||||
@@ -12,6 +13,7 @@ router.get(
|
||||
'/',
|
||||
useCollection('directus_activity'),
|
||||
sanitizeQuery,
|
||||
cacheMiddleware,
|
||||
asyncHandler(async (req, res) => {
|
||||
const service = new ActivityService({ accountability: req.accountability });
|
||||
const metaService = new MetaService({ accountability: req.accountability });
|
||||
|
||||
@@ -16,42 +16,46 @@ const redisClient = redis.createClient({
|
||||
password: env.REDIS_PASSWORD,
|
||||
});
|
||||
|
||||
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;
|
||||
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
|
||||
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
if (!redisClient) {
|
||||
throw new RedisNotFoundException('Redis client does not exist');
|
||||
}
|
||||
if (!req.query.TTL) return next();
|
||||
if (!req.query.dTTL) return next();
|
||||
|
||||
redisClient.get(key, (error, resultData) => {
|
||||
if (error) {
|
||||
throw new RedisNotFoundException('Error retreiving redis cache');
|
||||
}
|
||||
const key = req.url;
|
||||
const TTL = req.query.TTL;
|
||||
const checkDeath = req.query.dTTL;
|
||||
|
||||
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));
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
if (!redisClient) {
|
||||
throw new RedisNotFoundException('Redis client does not exist');
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
};
|
||||
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));
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
export default cacheMiddleware;
|
||||
|
||||
Reference in New Issue
Block a user