From 509504c7c8c171582a9ca5e7637a75678b079fac Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 1 May 2020 20:15:27 +0200 Subject: [PATCH] caching using cached-hafas-client see also derhuerst/hvv-rest@8db740b --- .gitignore | 1 + api.js | 32 +++++++++++++++++++++++++++++--- package.json | 2 ++ readme.md | 6 ++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e32ede0..33a78ae 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules npm-debug.log /package-lock.json +/dump.rdb diff --git a/api.js b/api.js index 3d44f86..fba85df 100644 --- a/api.js +++ b/api.js @@ -3,15 +3,41 @@ const createHafas = require('db-hafas') const createApi = require('hafas-rest-api') const createHealthCheck = require('hafas-client-health-check') +const {createClient: createRedis} = require('redis') +const withCache = require('cached-hafas-client') +const redisStore = require('cached-hafas-client/stores/redis') const pkg = require('./package.json') const stations = require('./routes/stations') const station = require('./routes/station') -const hafas = createHafas(pkg.name) - const berlinHbf = '8011160' -const healthCheck = createHealthCheck(hafas, berlinHbf) + +let hafas = createHafas(pkg.name) +let healthCheck = createHealthCheck(hafas, berlinHbf) + +if (process.env.REDIS_URL) { + const redis = createRedis({ + url: process.env.REDIS_URL, + }) + 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') + }) + }) + healthCheck = async () => ( + (await checkHafas()) === true && + (await checkRedis()) === true + ) +} const modifyRoutes = (routes) => { routes['/stations/:id'] = station diff --git a/package.json b/package.json index 5775165..9ae1e6c 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "node": ">=10" }, "dependencies": { + "cached-hafas-client": "^3.1.1", "cli-native": "^1.0.0", "db-hafas": "^5.0.2", "db-stations": "^3.0.0", @@ -29,6 +30,7 @@ "etag": "^1.8.1", "hafas-client-health-check": "^2.1.1", "hafas-rest-api": "^3.3.0", + "redis": "^3.0.2", "serve-buffer": "^2.0.0" }, "devDependencies": { diff --git a/readme.md b/readme.md index e8d34f5..2f07090 100644 --- a/readme.md +++ b/readme.md @@ -15,6 +15,8 @@ ## installing & running +`bvg-rest` expects a [Redis](https://redis.io/) server running on `127.0.0.1:6379` (default port), but you can set the `REDIS_URL` environment variable to change this. + ### via Docker A Docker image [is available as `derhuerst/db-rest:5`](https://hub.docker.com/r/derhuerst/db-rest:5). @@ -23,6 +25,8 @@ A Docker image [is available as `derhuerst/db-rest:5`](https://hub.docker.com/r/ docker run -d -p 3000:3000 derhuerst/db-rest:5 ``` +*Note:* The Docker image does not contain the Redis server. + ### manually ```shell @@ -30,6 +34,8 @@ git clone https://github.com/derhuerst/db-rest.git cd db-rest git checkout 5 npm install --production + +redis-server & npm start ```