From e643589440828f1a3961dfd53f4617675f514b7f Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 28 Nov 2021 13:38:16 +0100 Subject: [PATCH] redis -> ioredis, cached-hafas-client@4 --- api.js | 21 ++++++++++----------- package.json | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/api.js b/api.js index 7d11fea..cb0c5c9 100644 --- a/api.js +++ b/api.js @@ -3,7 +3,7 @@ const createHafas = require('db-hafas') const createApi = require('hafas-rest-api') const createHealthCheck = require('hafas-client-health-check') -const {createClient: createRedis} = require('redis') +const Redis = require('ioredis') const withCache = require('cached-hafas-client') const redisStore = require('cached-hafas-client/stores/redis') const {join: pathJoin} = require('path') @@ -20,21 +20,20 @@ let hafas = createHafas(pkg.name) let healthCheck = createHealthCheck(hafas, berlinHbf) if (process.env.REDIS_URL) { - const redis = createRedis({ - url: process.env.REDIS_URL, + const redis = new Redis(process.env.REDIS_URL || null) + hafas = withCache(hafas, redisStore(redis), { + cachePeriods: { + locations: 6 * 60 * 60 * 1000, // 6h + }, }) - redis.on('error', (err) => { - api.locals.logger.error(err) - }) - hafas = withCache(hafas, redisStore(redis)) const checkHafas = healthCheck const checkRedis = () => new Promise((resolve, reject) => { setTimeout(reject, 1000, new Error('didn\'t receive a PONG')) - redis.ping((err, res) => { - if (err) reject(err) - else resolve(res === 'PONG') - }) + redis.ping().then( + res => resolve(res === 'PONG'), + reject, + ) }) healthCheck = async () => ( (await checkHafas()) === true && diff --git a/package.json b/package.json index 4b42afa..e4b838e 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "node": ">=10" }, "dependencies": { - "cached-hafas-client": "^3.1.1", + "cached-hafas-client": "^4.0.4", "cli-native": "^1.0.0", "db-hafas": "^5.0.2", "db-stations": "^3.0.0", @@ -30,7 +30,7 @@ "etag": "^1.8.1", "hafas-client-health-check": "^2.1.1", "hafas-rest-api": "^3.7.0", - "redis": "^3.0.2", + "ioredis": "^4.28.1", "serve-buffer": "^2.0.0", "serve-static": "^1.14.1" },