Skip to content

Commit

Permalink
add redis cache to bin
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Apr 3, 2018
1 parent 39fb618 commit b889e4f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
38 changes: 37 additions & 1 deletion bin/update-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,45 @@
process.title = 'update-server'

const Updates = require('..')
const redis = require('redis')
const { promisify } = require('util')
const ms = require('ms')
const assert = require('assert')

//
// Args
//

const { TOKEN: token } = process.env
assert(token, 'TOKEN required')

//
// Cache
//

const client = redis.createClient()
const get = promisify(client.get).bind(client)
const set = promisify(client.set).bind(client)

const cache = {
async get (key) {
const json = await get(key)
return json && JSON.parse(json)
},
async set (key, value) {
const multi = client.multi()
multi.set(key, JSON.stringify(value))
multi.expire(key, ms('15m'))
const exec = promisify(multi.exec).bind(multi)
await exec()
}
}

//
// Go!
//

const updates = new Updates({ token })
const updates = new Updates({ token, cache })
const server = updates.listen(3000, () => {
console.log(`http://localhost:${server.address().port}`)
})
96 changes: 92 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"test": "prettier-standard '**/*.js' && standard && NODE_ENV=test nyc tap test/*.js"
},
"dependencies": {
"node-fetch": "^2.1.1"
"ms": "^2.1.1",
"node-fetch": "^2.1.1",
"redis": "^2.8.0"
},
"devDependencies": {
"nodemon": "^1.17.1",
Expand Down

0 comments on commit b889e4f

Please sign in to comment.