redis -> ioredis, cached-hafas-client@4

This commit is contained in:
Jannis R 2021-11-28 13:38:16 +01:00
parent 434b0df6c7
commit e643589440
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 12 additions and 13 deletions

21
api.js
View file

@ -3,7 +3,7 @@
const createHafas = require('db-hafas') const createHafas = require('db-hafas')
const createApi = require('hafas-rest-api') const createApi = require('hafas-rest-api')
const createHealthCheck = require('hafas-client-health-check') const createHealthCheck = require('hafas-client-health-check')
const {createClient: createRedis} = require('redis') const Redis = require('ioredis')
const withCache = require('cached-hafas-client') const withCache = require('cached-hafas-client')
const redisStore = require('cached-hafas-client/stores/redis') const redisStore = require('cached-hafas-client/stores/redis')
const {join: pathJoin} = require('path') const {join: pathJoin} = require('path')
@ -20,21 +20,20 @@ let hafas = createHafas(pkg.name)
let healthCheck = createHealthCheck(hafas, berlinHbf) let healthCheck = createHealthCheck(hafas, berlinHbf)
if (process.env.REDIS_URL) { if (process.env.REDIS_URL) {
const redis = createRedis({ const redis = new Redis(process.env.REDIS_URL || null)
url: process.env.REDIS_URL, 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 checkHafas = healthCheck
const checkRedis = () => new Promise((resolve, reject) => { const checkRedis = () => new Promise((resolve, reject) => {
setTimeout(reject, 1000, new Error('didn\'t receive a PONG')) setTimeout(reject, 1000, new Error('didn\'t receive a PONG'))
redis.ping((err, res) => { redis.ping().then(
if (err) reject(err) res => resolve(res === 'PONG'),
else resolve(res === 'PONG') reject,
}) )
}) })
healthCheck = async () => ( healthCheck = async () => (
(await checkHafas()) === true && (await checkHafas()) === true &&

View file

@ -22,7 +22,7 @@
"node": ">=10" "node": ">=10"
}, },
"dependencies": { "dependencies": {
"cached-hafas-client": "^3.1.1", "cached-hafas-client": "^4.0.4",
"cli-native": "^1.0.0", "cli-native": "^1.0.0",
"db-hafas": "^5.0.2", "db-hafas": "^5.0.2",
"db-stations": "^3.0.0", "db-stations": "^3.0.0",
@ -30,7 +30,7 @@
"etag": "^1.8.1", "etag": "^1.8.1",
"hafas-client-health-check": "^2.1.1", "hafas-client-health-check": "^2.1.1",
"hafas-rest-api": "^3.7.0", "hafas-rest-api": "^3.7.0",
"redis": "^3.0.2", "ioredis": "^4.28.1",
"serve-buffer": "^2.0.0", "serve-buffer": "^2.0.0",
"serve-static": "^1.14.1" "serve-static": "^1.14.1"
}, },