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 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 &&

View file

@ -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"
},