From 8b97c6d818b2bb87201c990de6751924bdbd72ca Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Tue, 8 Sep 2020 17:09:15 -0400 Subject: [PATCH] Clear middleware/service (temp) --- api/src/exceptions/invalid-cache-key.ts | 7 ---- api/src/middleware/check-cache.ts | 43 +-------------------- api/src/services/cache.ts | 50 ------------------------- 3 files changed, 1 insertion(+), 99 deletions(-) delete mode 100644 api/src/exceptions/invalid-cache-key.ts diff --git a/api/src/exceptions/invalid-cache-key.ts b/api/src/exceptions/invalid-cache-key.ts deleted file mode 100644 index b61510ddf7..0000000000 --- a/api/src/exceptions/invalid-cache-key.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { BaseException } from './base'; - -export class InvalidCacheKeyException extends BaseException { - constructor(message: string) { - super(message, 400, 'INVALID_CACHE_KEY'); - } -} diff --git a/api/src/middleware/check-cache.ts b/api/src/middleware/check-cache.ts index c0baeb528e..4e9d5ddacf 100644 --- a/api/src/middleware/check-cache.ts +++ b/api/src/middleware/check-cache.ts @@ -1,51 +1,10 @@ -/** - * Caching using redis - * and node caching - */ import { RequestHandler } from 'express'; import asyncHandler from 'express-async-handler'; import CacheService from '../services/cache'; -import { RedisNotFoundException } from '../exceptions'; import env from '../env'; const checkCacheMiddleware: 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 - // have used query as then can decide whather to use cache or not from api call - if (env.CACHE_ENABLED !== 'true') return next(); - - //key needs to have url, query and permissions - - const key = `${req.url}${req.query}${req.permissions}`; - - // we have two options here. Redis or node cache - if (env.CACHE_DRIVER === 'redis') { - const redis = require('redis'); - const redisClient = redis.createClient({ - enable_offline_queue: false, - host: env.CACHE_HOST, - port: env.CACHE_PORT, - password: env.CACHE_REDIS_PASSWORD, - }); - if (!redisClient) { - throw new RedisNotFoundException('Redis client does not exist'); - } - - redisClient.get(key, (error: string, resultData: string) => { - if (error) { - throw new RedisNotFoundException('Error retreiving redis cache'); - } - - if (resultData) { - const reponse = JSON.parse(resultData); - res.json(reponse); - } - }); - } else { - const cacheService = new CacheService(); - res.json(cacheService.getCache(key)); - } + if (env.CACHE_ENABLED !== true) return next(); return next(); }); diff --git a/api/src/services/cache.ts b/api/src/services/cache.ts index e9918dda37..023535367f 100644 --- a/api/src/services/cache.ts +++ b/api/src/services/cache.ts @@ -1,55 +1,5 @@ -/** cache service. - * Makes dealing with cache management easier - * in case a new cache is used in future - * sdtTTL is the amount of time a cache should be - * stored, in seconds, before being deleted. Keep - * checkPeriod is the amount of seconds for the - * autodelete to wait before checking - * if cache needs to be deleted. - * Have wrapped node cache so we can extend if needed - * could put redis cache in here too - */ -import NodeCache from 'node-cache'; -import redis from 'redis'; -import { InvalidCacheKeyException } from '../exceptions'; import env from '../env'; export default class CacheService { - apiCache: NodeCache; - constructor() { - // options found at https://github.com/node-cache/node-cache - this.apiCache = new NodeCache({ - stdTTL: env.CACHE_TTL, - checkperiod: env.CACHE_CHECK_LIVE, - useClones: false, - }); - } - // delete the cache with the given key. Casted to string by node-cache - // so might as well do it here too for consitancy - - async delCache(keys: string) { - if (!keys) { - throw new InvalidCacheKeyException('Keys was not provided for cache'); - } - this.apiCache.del(keys); - } - // attempt to get the cache based on the key, if it is empty then set it - - async getCache(key: string) { - const value = this.apiCache.get(key); - - if (value) { - return Promise.resolve(value); - } - } - - async setCache(key: string, data: {}) { - this.apiCache.set(key, data); - } - - // this flushes all data. important incase cache gets too full. - async flushCache() { - this.apiCache.flushAll(); - } }