mirror of
https://github.com/directus/directus.git
synced 2026-01-28 15:28:24 -05:00
changing the cache again
This commit is contained in:
@@ -68,12 +68,8 @@ if (env.NODE_ENV !== 'development') {
|
||||
}
|
||||
|
||||
// use the rate limiter - all routes for now
|
||||
app.use(rateLimiter);
|
||||
app.use('/auth', authRouter)
|
||||
|
||||
.use(authenticate)
|
||||
|
||||
.use('/activity', activityRouter)
|
||||
app.use('/activity', activityRouter)
|
||||
.use('/assets', assetsRouter)
|
||||
.use('/collections', collectionsRouter)
|
||||
.use('/extensions', extensionsRouter)
|
||||
|
||||
@@ -20,7 +20,6 @@ router.get(
|
||||
|
||||
const records = await service.readByQuery(req.sanitizedQuery);
|
||||
const meta = await metaService.getMetaForQuery(req.collection, req.sanitizedQuery);
|
||||
|
||||
return res.json({
|
||||
data: records || null,
|
||||
meta,
|
||||
|
||||
@@ -3,8 +3,18 @@ import asyncHandler from 'express-async-handler';
|
||||
import sanitizeQuery from '../middleware/sanitize-query';
|
||||
import cacheMiddleware from '../middleware/cache';
|
||||
import CollectionsService from '../services/collections';
|
||||
import CacheService from '../services/node-cache';
|
||||
import useCollection from '../middleware/use-collection';
|
||||
import MetaService from '../services/meta';
|
||||
import redis from 'redis';
|
||||
import env from '../env';
|
||||
|
||||
const redisClient = redis.createClient({
|
||||
enable_offline_queue: false,
|
||||
host: env.REDIS_HOST,
|
||||
port: env.REDIS_PORT,
|
||||
password: env.REDIS_PASSWORD,
|
||||
});
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -26,12 +36,16 @@ router.get(
|
||||
useCollection('directus_collections'),
|
||||
cacheMiddleware,
|
||||
asyncHandler(async (req, res) => {
|
||||
const key = req.url;
|
||||
const TTL = req.query.TTL;
|
||||
const TTLnum = Number(TTL);
|
||||
|
||||
const collectionsService = new CollectionsService({ accountability: req.accountability });
|
||||
const metaService = new MetaService({ accountability: req.accountability });
|
||||
|
||||
const collections = await collectionsService.readByQuery();
|
||||
const meta = await metaService.getMetaForQuery(req.collection, {});
|
||||
|
||||
redisClient.setex(key, TTLnum, JSON.stringify({ data: collections || null, meta }));
|
||||
res.json({ data: collections || null, meta });
|
||||
})
|
||||
);
|
||||
@@ -42,11 +56,22 @@ router.get(
|
||||
sanitizeQuery,
|
||||
cacheMiddleware,
|
||||
asyncHandler(async (req, res) => {
|
||||
const key = req.url;
|
||||
const TTL = req.query.TTL;
|
||||
const TTLnum = Number(TTL);
|
||||
const dTTL = Number(req.query.dTTL);
|
||||
|
||||
const collectionsService = new CollectionsService({ accountability: req.accountability });
|
||||
const collectionKey = req.params.collection.includes(',')
|
||||
? req.params.collection.split(',')
|
||||
: req.params.collection;
|
||||
const collection = await collectionsService.readByKey(collectionKey as any);
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
redisClient.setex(key, TTLnum, JSON.stringify({ data: collection || null }));
|
||||
} else {
|
||||
const cacheService = new CacheService(TTLnum, dTTL);
|
||||
cacheService.setCache(key, JSON.stringify({ data: collection || null }));
|
||||
}
|
||||
res.json({ data: collection || null });
|
||||
})
|
||||
);
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
*/
|
||||
import { RequestHandler } from 'express';
|
||||
import redis from 'redis';
|
||||
import NodeCache from 'node-cache';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import CacheService from '../services/node-cache';
|
||||
import { RedisNotFoundException } from '../exceptions';
|
||||
import { InvalidCacheKeyException } from '../exceptions';
|
||||
import env from '../env';
|
||||
import NodeCache from 'node-cache';
|
||||
|
||||
const redisClient = redis.createClient({
|
||||
enable_offline_queue: false,
|
||||
host: env.REDIS_HOST,
|
||||
@@ -25,12 +27,10 @@ const cacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
if (!req.query.TTL) return next();
|
||||
if (!req.query.dTTL) return next();
|
||||
|
||||
const key = req.url;
|
||||
const TTL = req.query.TTL;
|
||||
const checkDeath = req.query.dTTL;
|
||||
const TTLnumber = Number(req.query.TTL);
|
||||
const dTTL = Number(req.query.dTTL);
|
||||
|
||||
const TTLnum = Number(TTL);
|
||||
const cDnum = Number(checkDeath);
|
||||
const key = req.url;
|
||||
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_TYPE === 'redis') {
|
||||
@@ -44,18 +44,13 @@ const cacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
}
|
||||
|
||||
if (resultData) {
|
||||
res.send(resultData);
|
||||
}
|
||||
if (!resultData) {
|
||||
// set data and then return
|
||||
redisClient.setex(key, TTLnum, JSON.stringify(res.json));
|
||||
const reponse = JSON.parse(resultData);
|
||||
res.json(reponse);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// use the node cache
|
||||
const nodeCache = new CacheService(TTLnum, cDnum);
|
||||
|
||||
nodeCache.getCache(key, JSON.stringify(res.json));
|
||||
const cacheService = new CacheService(TTLnumber, dTTL);
|
||||
res.json(cacheService.getCache(key));
|
||||
}
|
||||
|
||||
return next();
|
||||
|
||||
@@ -34,19 +34,16 @@ export default class CacheService {
|
||||
}
|
||||
// attempt to get the cache based on the key, if it is empty then set it
|
||||
|
||||
async getCache(key: string, setData: string) {
|
||||
if (!setData) {
|
||||
throw new InvalidCacheKeyException('No response data was provided for cache');
|
||||
}
|
||||
// first get the value
|
||||
async getCache(key: string) {
|
||||
const value = this.apiCache.get(key);
|
||||
|
||||
if (value) {
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
}
|
||||
|
||||
this.apiCache.set(key, setData);
|
||||
return setData;
|
||||
async setCache(key: string, data: {}) {
|
||||
this.apiCache.set(key, data);
|
||||
}
|
||||
|
||||
// this flushes all data. important incase cache gets too full.
|
||||
|
||||
Reference in New Issue
Block a user