From efcb711d920118878deb24b616bf81a55c7852fb Mon Sep 17 00:00:00 2001 From: Tanya Byrne Date: Tue, 25 Aug 2020 13:23:54 +0100 Subject: [PATCH] changing the cache again --- api/src/app.ts | 6 +----- api/src/controllers/activity.ts | 1 - api/src/controllers/collections.ts | 27 ++++++++++++++++++++++++++- api/src/middleware/cache.ts | 25 ++++++++++--------------- api/src/services/node-cache.ts | 11 ++++------- 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 56738b8b0b..ca84ff95c5 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -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) diff --git a/api/src/controllers/activity.ts b/api/src/controllers/activity.ts index d56e424019..908e1aa1bf 100644 --- a/api/src/controllers/activity.ts +++ b/api/src/controllers/activity.ts @@ -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, diff --git a/api/src/controllers/collections.ts b/api/src/controllers/collections.ts index 25b4a6e209..689ae080cf 100644 --- a/api/src/controllers/collections.ts +++ b/api/src/controllers/collections.ts @@ -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 }); }) ); diff --git a/api/src/middleware/cache.ts b/api/src/middleware/cache.ts index a415fc90ef..43b4dbffbc 100644 --- a/api/src/middleware/cache.ts +++ b/api/src/middleware/cache.ts @@ -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(); diff --git a/api/src/services/node-cache.ts b/api/src/services/node-cache.ts index 4fb72d1b0b..b25803ab79 100644 --- a/api/src/services/node-cache.ts +++ b/api/src/services/node-cache.ts @@ -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.