From 47f6409bb8bc49a50027b9579bb651c0506b6912 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 25 Apr 2019 11:27:13 +0200 Subject: Use apicache instead of our broken implementation --- server/lib/job-queue/job-queue.ts | 2 +- server/lib/redis.ts | 12 +++++-- server/middlewares/cache.ts | 73 +++++---------------------------------- 3 files changed, 19 insertions(+), 68 deletions(-) (limited to 'server') diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index f09eb6ff1..3c810da98 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -71,7 +71,7 @@ class JobQueue { this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST const queueOptions = { prefix: this.jobRedisPrefix, - redis: Redis.getRedisClient(), + redis: Redis.getRedisClientOptions(), settings: { maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts } diff --git a/server/lib/redis.ts b/server/lib/redis.ts index b4044bf0f..f77d0b62c 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -31,7 +31,7 @@ class Redis { if (this.initialized === true) return this.initialized = true - this.client = createClient(Redis.getRedisClient()) + this.client = createClient(Redis.getRedisClientOptions()) this.client.on('error', err => { logger.error('Error in Redis client.', { err }) @@ -45,7 +45,7 @@ class Redis { this.prefix = 'redis-' + WEBSERVER.HOST + '-' } - static getRedisClient () { + static getRedisClientOptions () { return Object.assign({}, (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, @@ -55,6 +55,14 @@ class Redis { ) } + getClient () { + return this.client + } + + getPrefix () { + return this.prefix + } + /************* Forgot password *************/ async setResetPasswordVerificationString (userId: number) { diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index 091c82d92..ef8611875 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts @@ -1,73 +1,16 @@ -import * as express from 'express' -import * as AsyncLock from 'async-lock' -import { parseDurationToMs } from '../helpers/core-utils' import { Redis } from '../lib/redis' -import { logger } from '../helpers/logger' +import * as apicache from 'apicache' -const lock = new AsyncLock({ timeout: 5000 }) +// Ensure Redis is initialized +Redis.Instance.init() -function cacheRoute (lifetimeArg: string | number) { - const lifetime = parseDurationToMs(lifetimeArg) - - return async function (req: express.Request, res: express.Response, next: express.NextFunction) { - const redisKey = Redis.Instance.generateCachedRouteKey(req) - - try { - await lock.acquire(redisKey, async (done) => { - const cached = await Redis.Instance.getCachedRoute(req) - - // Not cached - if (!cached) { - logger.debug('No cached results for route %s.', req.originalUrl) - - const sendSave = res.send.bind(res) - const redirectSave = res.redirect.bind(res) - - res.send = (body) => { - if (res.statusCode >= 200 && res.statusCode < 400) { - const contentType = res.get('content-type') - - Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) - .then(() => done()) - .catch(err => { - logger.error('Cannot cache route.', { err }) - return done(err) - }) - } else { - done() - } - - return sendSave(body) - } - - res.redirect = url => { - done() - - return redirectSave(url) - } - - return next() - } - - if (cached.contentType) res.set('content-type', cached.contentType) - - if (cached.statusCode) { - const statusCode = parseInt(cached.statusCode, 10) - if (!isNaN(statusCode)) res.status(statusCode) - } - - logger.debug('Use cached result for %s.', req.originalUrl) - res.send(cached.body).end() - - return done() - }) - } catch (err) { - logger.error('Cannot serve cached route.', { err }) - return next() - } - } +const options = { + redisClient: Redis.Instance.getClient(), + appendKey: () => Redis.Instance.getPrefix() } +const cacheRoute = apicache.options(options).middleware + // --------------------------------------------------------------------------- export { -- cgit v1.2.3